"503 Maintenance" error handling

refs https://github.com/TryGhost/Ghost/issues/6976
- adds custom `MaintenanceError` and associated error checking functions
- updates app route and notifications service to handle `503` errors via the `upgrade-status` service
This commit is contained in:
Kevin Ansfield 2016-07-08 14:54:36 +01:00
parent eb2a0359cf
commit 3c92c171f9
5 changed files with 54 additions and 1 deletions

View File

@ -9,6 +9,14 @@ const {
} = Ember; } = Ember;
/* jshint unused:false */ /* jshint unused:false */
function maintenanceResponse() {
return new Mirage.Response(503, {}, {
errors: [{
errorType: 'Maintenance'
}]
});
}
function versionMismatchResponse() { function versionMismatchResponse() {
return new Mirage.Response(400, {}, { return new Mirage.Response(400, {}, {
errors: [{ errors: [{

View File

@ -176,6 +176,14 @@ export default Route.extend(ApplicationRouteMixin, ShortcutsRoute, {
this.get('upgradeStatus').requireUpgrade(); this.get('upgradeStatus').requireUpgrade();
return false; return false;
case 'Maintenance':
if (transition) {
transition.abort();
}
this.get('upgradeStatus').maintenanceAlert();
return false;
default: default:
this.get('notifications').showAPIError(error); this.get('notifications').showAPIError(error);
// don't show the 500 page if we weren't navigating // don't show the 500 page if we weren't navigating

View File

@ -70,6 +70,24 @@ export function isUnsupportedMediaTypeError(errorOrStatus) {
} }
} }
/* Maintenance error */
export function MaintenanceError(errors) {
AjaxError.call(this, errors, 'Ghost is currently undergoing maintenance, please wait a moment then retry.');
}
MaintenanceError.prototype = Object.create(AjaxError.prototype);
export function isMaintenanceError(errorOrStatus) {
if (isAjaxError(errorOrStatus)) {
return errorOrStatus instanceof MaintenanceError;
} else if (errorOrStatus && get(errorOrStatus, 'isAdapterError')) {
return get(errorOrStatus, 'errors.firstObject.errorType') === 'Maintenance';
} else {
return errorOrStatus === 503;
}
}
/* end: custom error types */ /* end: custom error types */
export default AjaxService.extend({ export default AjaxService.extend({
@ -99,6 +117,8 @@ export default AjaxService.extend({
return new RequestEntityTooLargeError(payload.errors); return new RequestEntityTooLargeError(payload.errors);
} else if (this.isUnsupportedMediaTypeError(status, headers, payload)) { } else if (this.isUnsupportedMediaTypeError(status, headers, payload)) {
return new UnsupportedMediaTypeError(payload.errors); return new UnsupportedMediaTypeError(payload.errors);
} else if (this.isMaintenanceError(status, headers, payload)) {
return new MaintenanceError(payload.errors);
} }
return this._super(...arguments); return this._super(...arguments);
@ -136,5 +156,9 @@ export default AjaxService.extend({
isUnsupportedMediaTypeError(status/*, headers, payload */) { isUnsupportedMediaTypeError(status/*, headers, payload */) {
return isUnsupportedMediaTypeError(status); return isUnsupportedMediaTypeError(status);
},
isMaintenanceError(status, headers, payload) {
return isMaintenanceError(status, payload);
} }
}); });

View File

@ -4,9 +4,12 @@ import {A as emberA, isEmberArray} from 'ember-array/utils';
import get from 'ember-metal/get'; import get from 'ember-metal/get';
import set from 'ember-metal/set'; import set from 'ember-metal/set';
import injectService from 'ember-service/inject'; import injectService from 'ember-service/inject';
import {isVersionMismatchError} from 'ghost-admin/services/ajax';
import {isBlank} from 'ember-utils'; import {isBlank} from 'ember-utils';
import {dasherize} from 'ember-string'; import {dasherize} from 'ember-string';
import {
isMaintenanceError,
isVersionMismatchError
} from 'ghost-admin/services/ajax';
// Notification keys take the form of "noun.verb.message", eg: // Notification keys take the form of "noun.verb.message", eg:
// //
@ -88,6 +91,9 @@ export default Service.extend({
// handle "global" errors // handle "global" errors
if (isVersionMismatchError(resp)) { if (isVersionMismatchError(resp)) {
return this.get('upgradeStatus').requireUpgrade(); return this.get('upgradeStatus').requireUpgrade();
} else if (isMaintenanceError(resp)) {
console.log(this.get('upgradeStatus'));
return this.get('upgradeStatus').maintenanceAlert();
} }
// loop over Ember Data / ember-ajax errors object // loop over Ember Data / ember-ajax errors object

View File

@ -6,6 +6,13 @@ export default Service.extend({
notifications: injectService(), notifications: injectService(),
maintenanceAlert() {
this.get('notifications').showAlert(
'Sorry, Ghost is currently undergoing maintenance, please wait a moment then try again.',
{type: 'error', key: 'api-error.under-maintenance'}
);
},
requireUpgrade() { requireUpgrade() {
this.set('isRequired', true); this.set('isRequired', true);
this.get('notifications').showAlert( this.get('notifications').showAlert(