Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
"503 Maintenance" error handling
Browse files Browse the repository at this point in the history
refs TryGhost/Ghost#6976
- adds custom `MaintenanceError` and associated error checking functions
- updates app route and notifications service to handle `503` errors via the `upgrade-status` service
  • Loading branch information
kevinansfield committed Jul 8, 2016
1 parent bfafc8e commit 2cf2ba9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app/mirage/config.js
Expand Up @@ -9,6 +9,14 @@ const {
} = Ember;

/* jshint unused:false */
function maintenanceResponse() {
return new Mirage.Response(503, {}, {
errors: [{
errorType: 'Maintenance'
}]
});
}

function versionMismatchResponse() {
return new Mirage.Response(400, {}, {
errors: [{
Expand Down
8 changes: 8 additions & 0 deletions app/routes/application.js
Expand Up @@ -176,6 +176,14 @@ export default Route.extend(ApplicationRouteMixin, ShortcutsRoute, {
this.get('upgradeStatus').requireUpgrade();
return false;

case 'Maintenance':
if (transition) {
transition.abort();
}

this.get('upgradeStatus').maintenanceAlert();
return false;

default:
this.get('notifications').showAPIError(error);
// don't show the 500 page if we weren't navigating
Expand Down
24 changes: 24 additions & 0 deletions app/services/ajax.js
Expand Up @@ -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 */

export default AjaxService.extend({
Expand Down Expand Up @@ -99,6 +117,8 @@ export default AjaxService.extend({
return new RequestEntityTooLargeError(payload.errors);
} else if (this.isUnsupportedMediaTypeError(status, headers, payload)) {
return new UnsupportedMediaTypeError(payload.errors);
} else if (this.isMaintenanceError(status, headers, payload)) {
return new MaintenanceError(payload.errors);
}

return this._super(...arguments);
Expand Down Expand Up @@ -136,5 +156,9 @@ export default AjaxService.extend({

isUnsupportedMediaTypeError(status/*, headers, payload */) {
return isUnsupportedMediaTypeError(status);
},

isMaintenanceError(status, headers, payload) {
return isMaintenanceError(status, payload);
}
});
8 changes: 7 additions & 1 deletion app/services/notifications.js
Expand Up @@ -4,9 +4,12 @@ import {A as emberA, isEmberArray} from 'ember-array/utils';
import get from 'ember-metal/get';
import set from 'ember-metal/set';
import injectService from 'ember-service/inject';
import {isVersionMismatchError} from 'ghost-admin/services/ajax';
import {isBlank} from 'ember-utils';
import {dasherize} from 'ember-string';
import {
isMaintenanceError,
isVersionMismatchError
} from 'ghost-admin/services/ajax';

// Notification keys take the form of "noun.verb.message", eg:
//
Expand Down Expand Up @@ -88,6 +91,9 @@ export default Service.extend({
// handle "global" errors
if (isVersionMismatchError(resp)) {
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
Expand Down
7 changes: 7 additions & 0 deletions app/services/upgrade-status.js
Expand Up @@ -6,6 +6,13 @@ export default Service.extend({

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() {
this.set('isRequired', true);
this.get('notifications').showAlert(
Expand Down

0 comments on commit 2cf2ba9

Please sign in to comment.