Skip to content

Commit 13df2e4

Browse files
committed
"503 Maintenance" error handling
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
1 parent 690dc9d commit 13df2e4

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

app/mirage/config.js

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ const {
99
} = Ember;
1010

1111
/* jshint unused:false */
12+
function maintenanceResponse() {
13+
return new Mirage.Response(503, {}, {
14+
errors: [{
15+
errorType: 'Maintenance'
16+
}]
17+
});
18+
}
19+
1220
function versionMismatchResponse() {
1321
return new Mirage.Response(400, {}, {
1422
errors: [{

app/routes/application.js

+8
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ export default Route.extend(ApplicationRouteMixin, ShortcutsRoute, {
176176
this.get('upgradeStatus').requireUpgrade();
177177
return false;
178178

179+
case 'Maintenance':
180+
if (transition) {
181+
transition.abort();
182+
}
183+
184+
this.get('upgradeStatus').maintenanceAlert();
185+
return false;
186+
179187
default:
180188
this.get('notifications').showAPIError(error);
181189
// don't show the 500 page if we weren't navigating

app/services/ajax.js

+24
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,24 @@ export function isUnsupportedMediaTypeError(errorOrStatus) {
7070
}
7171
}
7272

73+
/* Maintenance error */
74+
75+
export function MaintenanceError(errors) {
76+
AjaxError.call(this, errors, 'Ghost is currently undergoing maintenance, please wait a moment then retry.');
77+
}
78+
79+
MaintenanceError.prototype = Object.create(AjaxError.prototype);
80+
81+
export function isMaintenanceError(errorOrStatus) {
82+
if (isAjaxError(errorOrStatus)) {
83+
return errorOrStatus instanceof MaintenanceError;
84+
} else if (errorOrStatus && get(errorOrStatus, 'isAdapterError')) {
85+
return get(errorOrStatus, 'errors.firstObject.errorType') === 'Maintenance';
86+
} else {
87+
return errorOrStatus === 503;
88+
}
89+
}
90+
7391
/* end: custom error types */
7492

7593
export default AjaxService.extend({
@@ -99,6 +117,8 @@ export default AjaxService.extend({
99117
return new RequestEntityTooLargeError(payload.errors);
100118
} else if (this.isUnsupportedMediaTypeError(status, headers, payload)) {
101119
return new UnsupportedMediaTypeError(payload.errors);
120+
} else if (this.isMaintenanceError(status, headers, payload)) {
121+
return new MaintenanceError(payload.errors);
102122
}
103123

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

137157
isUnsupportedMediaTypeError(status/*, headers, payload */) {
138158
return isUnsupportedMediaTypeError(status);
159+
},
160+
161+
isMaintenanceError(status, headers, payload) {
162+
return isMaintenanceError(status, payload);
139163
}
140164
});

app/services/notifications.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import {A as emberA, isEmberArray} from 'ember-array/utils';
44
import get from 'ember-metal/get';
55
import set from 'ember-metal/set';
66
import injectService from 'ember-service/inject';
7-
import {isVersionMismatchError} from 'ghost-admin/services/ajax';
87
import {isBlank} from 'ember-utils';
98
import {dasherize} from 'ember-string';
9+
import {
10+
isMaintenanceError,
11+
isVersionMismatchError
12+
} from 'ghost-admin/services/ajax';
1013

1114
// Notification keys take the form of "noun.verb.message", eg:
1215
//
@@ -88,6 +91,9 @@ export default Service.extend({
8891
// handle "global" errors
8992
if (isVersionMismatchError(resp)) {
9093
return this.get('upgradeStatus').requireUpgrade();
94+
} else if (isMaintenanceError(resp)) {
95+
console.log(this.get('upgradeStatus'));
96+
return this.get('upgradeStatus').maintenanceAlert();
9197
}
9298

9399
// loop over Ember Data / ember-ajax errors object

app/services/upgrade-status.js

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ export default Service.extend({
66

77
notifications: injectService(),
88

9+
maintenanceAlert() {
10+
this.get('notifications').showAlert(
11+
'Sorry, Ghost is currently undergoing maintenance, please wait a moment then try again.',
12+
{type: 'error', key: 'api-error.under-maintenance'}
13+
);
14+
},
15+
916
requireUpgrade() {
1017
this.set('isRequired', true);
1118
this.get('notifications').showAlert(

0 commit comments

Comments
 (0)