Skip to content

Commit

Permalink
Handled ServerError to show a custom error message in the admin (#1…
Browse files Browse the repository at this point in the history
…8709)

refs TryGhost/Product#4040

Handled `ServerError` to show a custom error message in the admin when
an unhandled server error occurs (i.e 504). Supersedes
#18703
  • Loading branch information
mike182uk authored Oct 20, 2023
1 parent 36c7805 commit b4d2dd0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 26 deletions.
22 changes: 0 additions & 22 deletions ghost/admin/app/services/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,22 +178,6 @@ export function isEmailError(errorOrStatus, payload) {
}
}

/* Gateway timeout error */

export class GatewayTimeoutError extends AjaxError {
constructor(payload) {
super(payload, 'Server is currently unavailable, please wait a moment then retry.', 504);
}
}

export function isGatewayTimeoutError(errorOrStatus) {
if (isAjaxError(errorOrStatus)) {
return errorOrStatus instanceof GatewayTimeoutError;
} else {
return errorOrStatus === 504;
}
}

/* end: custom error types */

export class AcceptedResponse {
Expand Down Expand Up @@ -342,8 +326,6 @@ class ajaxService extends AjaxService {
return new EmailError(payload);
} else if (this.isAcceptedResponse(status)) {
return new AcceptedResponse(payload);
} else if (this.isGatewayTimeoutError(status)) {
return new GatewayTimeoutError(payload);
}

let isGhostRequest = GHOST_REQUEST.test(request.url);
Expand Down Expand Up @@ -422,10 +404,6 @@ class ajaxService extends AjaxService {
return isEmailError(status, payload);
}

isGatewayTimeoutError(status) {
return isGatewayTimeoutError(status);
}

isAcceptedResponse(status) {
return isAcceptedResponse(status);
}
Expand Down
8 changes: 6 additions & 2 deletions ghost/admin/app/services/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ const GENERIC_ERROR_NAMES = [
'ReferenceError',
'SyntaxError',
'TypeError',
'URIError'
'URIError',
'ServerError'
];

export const GENERIC_ERROR_MESSAGE = 'An unexpected error occurred, please try again.';
Expand Down Expand Up @@ -176,7 +177,10 @@ export default class NotificationsService extends Service {

let msg = options.defaultErrorText || GENERIC_ERROR_MESSAGE;

if (resp?.name && GENERIC_ERROR_NAMES.includes(resp.name)) {
if (
resp?.name && GENERIC_ERROR_NAMES.includes(resp.name) ||
resp?.constructor && GENERIC_ERROR_NAMES.includes(resp.constructor.name)
) {
msg = GENERIC_ERROR_MESSAGE;
} else if (resp instanceof String) {
msg = resp;
Expand Down
4 changes: 2 additions & 2 deletions ghost/admin/tests/acceptance/error-handling-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('Acceptance: Error Handling', function () {

expect(findAll('.gh-alert').length).to.equal(1);
expect(find('.gh-alert').textContent).to.not.match(/html>/);
expect(find('.gh-alert').textContent).to.match(/Server is currently unavailable, please wait a moment then retry./);
expect(find('.gh-alert').textContent).to.match(/An unexpected error occurred, please try again./);
});

it('handles ember-ajax HTML response', async function () {
Expand All @@ -118,7 +118,7 @@ describe('Acceptance: Error Handling', function () {

expect(findAll('.gh-alert').length).to.equal(1);
expect(find('.gh-alert').textContent).to.not.match(/html>/);
expect(find('.gh-alert').textContent).to.match(/Server is currently unavailable, please wait a moment then retry./);
expect(find('.gh-alert').textContent).to.match(/An unexpected error occurred, please try again./);
});
});
});

0 comments on commit b4d2dd0

Please sign in to comment.