2017-05-29 21:50:03 +03:00
|
|
|
import AjaxService from 'ember-ajax/services/ajax';
|
2022-02-03 01:11:11 +03:00
|
|
|
import classic from 'ember-classic-decorator';
|
2017-05-29 21:50:03 +03:00
|
|
|
import config from 'ghost-admin/config/environment';
|
2021-06-30 16:51:40 +03:00
|
|
|
import moment from 'moment';
|
2021-07-14 14:27:51 +03:00
|
|
|
import {AjaxError, isAjaxError, isForbiddenError} from 'ember-ajax/errors';
|
2021-06-30 16:51:40 +03:00
|
|
|
import {captureMessage} from '@sentry/browser';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {get} from '@ember/object';
|
|
|
|
import {isArray as isEmberArray} from '@ember/array';
|
|
|
|
import {isNone} from '@ember/utils';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2021-06-30 16:51:40 +03:00
|
|
|
import {timeout} from 'ember-concurrency';
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2018-04-16 19:55:21 +03:00
|
|
|
const JSON_CONTENT_TYPE = 'application/json';
|
|
|
|
const GHOST_REQUEST = /\/ghost\/api\//;
|
2016-09-26 19:59:04 +03:00
|
|
|
|
|
|
|
function isJSONContentType(header) {
|
2016-10-03 21:08:23 +03:00
|
|
|
if (!header || isNone(header)) {
|
2016-09-26 19:59:04 +03:00
|
|
|
return false;
|
|
|
|
}
|
2018-04-16 19:55:21 +03:00
|
|
|
return header.indexOf(JSON_CONTENT_TYPE) === 0;
|
2016-09-26 19:59:04 +03:00
|
|
|
}
|
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
/* Version mismatch error */
|
|
|
|
|
2018-09-24 12:42:49 +03:00
|
|
|
export class VersionMismatchError extends AjaxError {
|
|
|
|
constructor(payload) {
|
|
|
|
super(payload, 'API server is running a newer version of Ghost, please upgrade.');
|
|
|
|
}
|
2016-06-30 17:45:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isVersionMismatchError(errorOrStatus, payload) {
|
|
|
|
if (isAjaxError(errorOrStatus)) {
|
|
|
|
return errorOrStatus instanceof VersionMismatchError;
|
|
|
|
} else {
|
2019-03-25 14:29:14 +03:00
|
|
|
return get(payload || {}, 'errors.firstObject.type') === 'VersionMismatchError';
|
2016-06-30 17:45:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 12:42:49 +03:00
|
|
|
/* Server unreachable error */
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2018-09-24 12:42:49 +03:00
|
|
|
export class ServerUnreachableError extends AjaxError {
|
|
|
|
constructor(payload) {
|
|
|
|
super(payload, 'Server was unreachable');
|
|
|
|
}
|
2016-06-14 14:46:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isServerUnreachableError(error) {
|
|
|
|
if (isAjaxError(error)) {
|
|
|
|
return error instanceof ServerUnreachableError;
|
|
|
|
} else {
|
|
|
|
return error === 0 || error === '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 12:42:49 +03:00
|
|
|
/* Request entity too large error */
|
2016-02-26 16:25:47 +03:00
|
|
|
|
2018-09-24 12:42:49 +03:00
|
|
|
export class RequestEntityTooLargeError extends AjaxError {
|
|
|
|
constructor(payload) {
|
|
|
|
super(payload, 'Request is larger than the maximum file size the server allows');
|
|
|
|
}
|
|
|
|
}
|
2016-05-22 11:20:02 +03:00
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
export function isRequestEntityTooLargeError(errorOrStatus) {
|
|
|
|
if (isAjaxError(errorOrStatus)) {
|
|
|
|
return errorOrStatus instanceof RequestEntityTooLargeError;
|
2016-05-22 11:20:02 +03:00
|
|
|
} else {
|
2016-06-30 17:45:02 +03:00
|
|
|
return errorOrStatus === 413;
|
2016-05-22 11:20:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
/* Unsupported media type error */
|
|
|
|
|
2018-09-24 12:42:49 +03:00
|
|
|
export class UnsupportedMediaTypeError extends AjaxError {
|
|
|
|
constructor(payload) {
|
|
|
|
super(payload, 'Request contains an unknown or unsupported file type.');
|
|
|
|
}
|
2016-02-26 16:25:47 +03:00
|
|
|
}
|
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
export function isUnsupportedMediaTypeError(errorOrStatus) {
|
|
|
|
if (isAjaxError(errorOrStatus)) {
|
|
|
|
return errorOrStatus instanceof UnsupportedMediaTypeError;
|
2016-05-22 11:20:02 +03:00
|
|
|
} else {
|
2016-06-30 17:45:02 +03:00
|
|
|
return errorOrStatus === 415;
|
2016-05-22 11:20:02 +03:00
|
|
|
}
|
|
|
|
}
|
2016-04-12 14:34:40 +03:00
|
|
|
|
2016-07-08 16:54:36 +03:00
|
|
|
/* Maintenance error */
|
|
|
|
|
2018-09-24 12:42:49 +03:00
|
|
|
export class MaintenanceError extends AjaxError {
|
|
|
|
constructor(payload) {
|
|
|
|
super(payload, 'Ghost is currently undergoing maintenance, please wait a moment then retry.');
|
|
|
|
}
|
2016-07-08 16:54:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isMaintenanceError(errorOrStatus) {
|
|
|
|
if (isAjaxError(errorOrStatus)) {
|
|
|
|
return errorOrStatus instanceof MaintenanceError;
|
|
|
|
} else {
|
|
|
|
return errorOrStatus === 503;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 21:22:20 +03:00
|
|
|
/* Theme validation error */
|
|
|
|
|
2018-09-24 12:42:49 +03:00
|
|
|
export class ThemeValidationError extends AjaxError {
|
|
|
|
constructor(payload) {
|
|
|
|
super(payload, 'Theme is not compatible or contains errors.');
|
|
|
|
}
|
2016-08-24 21:22:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isThemeValidationError(errorOrStatus, payload) {
|
|
|
|
if (isAjaxError(errorOrStatus)) {
|
|
|
|
return errorOrStatus instanceof ThemeValidationError;
|
|
|
|
} else {
|
2019-03-25 14:29:14 +03:00
|
|
|
return get(payload || {}, 'errors.firstObject.type') === 'ThemeValidationError';
|
2016-08-24 21:22:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 16:57:42 +03:00
|
|
|
/* Host limit reached/exceeded error */
|
|
|
|
|
|
|
|
export class HostLimitError extends AjaxError {
|
|
|
|
constructor(payload) {
|
|
|
|
super(payload, 'A hosting plan limit was reached or exceeded.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isHostLimitError(errorOrStatus, payload) {
|
|
|
|
if (isAjaxError(errorOrStatus)) {
|
|
|
|
return errorOrStatus instanceof HostLimitError;
|
|
|
|
} else {
|
|
|
|
return get(payload || {}, 'errors.firstObject.type') === 'HostLimitError';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 20:35:13 +03:00
|
|
|
export class EmailError extends AjaxError {
|
|
|
|
constructor(payload) {
|
|
|
|
super(payload, 'Please verify your email settings');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isEmailError(errorOrStatus, payload) {
|
|
|
|
if (isAjaxError(errorOrStatus)) {
|
|
|
|
return errorOrStatus instanceof EmailError;
|
|
|
|
} else {
|
|
|
|
return get(payload || {}, 'errors.firstObject.type') === 'EmailError';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
/* end: custom error types */
|
|
|
|
|
2020-12-09 22:32:31 +03:00
|
|
|
export class AcceptedResponse {
|
|
|
|
constructor(data) {
|
|
|
|
this.data = data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isAcceptedResponse(errorOrStatus) {
|
|
|
|
if (errorOrStatus === 202) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-03 01:11:11 +03:00
|
|
|
@classic
|
|
|
|
class ajaxService extends AjaxService {
|
2022-02-03 22:04:43 +03:00
|
|
|
@service config;
|
|
|
|
@service session;
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2018-10-05 21:46:33 +03:00
|
|
|
// flag to tell our ESA authenticator not to try an invalidate DELETE request
|
|
|
|
// because it's been triggered by this service's 401 handling which means the
|
|
|
|
// DELETE would fail and get stuck in an infinite loop
|
|
|
|
// TODO: find a more elegant way to handle this
|
2022-02-03 01:11:11 +03:00
|
|
|
skipSessionDeletion = false;
|
2018-10-05 21:46:33 +03:00
|
|
|
|
2019-05-07 16:54:41 +03:00
|
|
|
get headers() {
|
2019-08-12 11:11:10 +03:00
|
|
|
return {
|
|
|
|
'X-Ghost-Version': config.APP.version,
|
|
|
|
'App-Pragma': 'no-cache'
|
|
|
|
};
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2019-02-26 06:38:00 +03:00
|
|
|
init() {
|
2022-02-03 01:11:11 +03:00
|
|
|
super.init(...arguments);
|
2019-02-26 06:38:00 +03:00
|
|
|
if (this.isTesting === undefined) {
|
|
|
|
this.isTesting = config.environment === 'test';
|
|
|
|
}
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2019-02-26 06:38:00 +03:00
|
|
|
|
2021-06-30 16:51:40 +03:00
|
|
|
async _makeRequest(hash) {
|
2022-03-08 06:36:31 +03:00
|
|
|
// ember-ajax recognizes `application/vnd.api+json` as a JSON-API request
|
2021-06-30 16:51:40 +03:00
|
|
|
// and formats appropriately, we want to handle `application/json` the same
|
2016-10-03 21:08:23 +03:00
|
|
|
if (isJSONContentType(hash.contentType) && hash.type !== 'GET') {
|
2016-09-26 19:59:04 +03:00
|
|
|
if (typeof hash.data === 'object') {
|
|
|
|
hash.data = JSON.stringify(hash.data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:46:33 +03:00
|
|
|
hash.withCredentials = true;
|
2016-10-17 16:32:22 +03:00
|
|
|
|
2021-06-30 16:51:40 +03:00
|
|
|
// attempt retries for 15 seconds in two situations:
|
|
|
|
// 1. Server Unreachable error from the browser (code 0), typically from short internet blips
|
|
|
|
// 2. Maintenance error from Ghost, upgrade in progress so API is temporarily unavailable
|
|
|
|
|
|
|
|
let success = false;
|
|
|
|
let errorName = null;
|
|
|
|
let attempts = 0;
|
|
|
|
let startTime = new Date();
|
|
|
|
let retryingMs = 0;
|
|
|
|
const maxRetryingMs = 15_000;
|
|
|
|
const retryPeriods = [500, 1000];
|
|
|
|
const retryErrorChecks = [this.isServerUnreachableError, this.isMaintenanceError];
|
|
|
|
|
|
|
|
const getErrorData = () => {
|
|
|
|
const data = {
|
|
|
|
errorName,
|
|
|
|
attempts,
|
|
|
|
totalSeconds: moment().diff(moment(startTime), 'seconds')
|
|
|
|
};
|
|
|
|
if (this._responseServer) {
|
|
|
|
data.server = this._responseServer;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2022-02-03 01:11:11 +03:00
|
|
|
const makeRequest = super._makeRequest.bind(this);
|
2021-06-30 16:51:40 +03:00
|
|
|
|
|
|
|
while (retryingMs <= maxRetryingMs && !success) {
|
|
|
|
try {
|
|
|
|
const result = await makeRequest(hash);
|
|
|
|
success = true;
|
|
|
|
|
|
|
|
if (attempts !== 0 && this.config.get('sentry_dsn')) {
|
|
|
|
captureMessage('Request took multiple attempts', {extra: getErrorData()});
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
} catch (error) {
|
|
|
|
errorName = error.response?.constructor?.name;
|
|
|
|
retryingMs = (new Date()) - startTime;
|
|
|
|
|
|
|
|
// avoid retries in tests because it slows things down and is not expected in mocks
|
|
|
|
// isTesting can be overridden in individual tests if required
|
|
|
|
if (this.isTesting) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retryErrorChecks.some(check => check(error.response)) && retryingMs <= maxRetryingMs) {
|
|
|
|
await timeout(retryPeriods[attempts] || retryPeriods[retryPeriods.length - 1]);
|
|
|
|
attempts += 1;
|
|
|
|
} else if (attempts > 0 && this.config.get('sentry_dsn')) {
|
|
|
|
captureMessage('Request failed after multiple attempts', {extra: getErrorData()});
|
|
|
|
throw error;
|
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2016-09-26 19:59:04 +03:00
|
|
|
|
2018-04-16 19:55:21 +03:00
|
|
|
handleResponse(status, headers, payload, request) {
|
2016-06-30 17:45:02 +03:00
|
|
|
if (this.isVersionMismatchError(status, headers, payload)) {
|
2017-11-04 01:59:39 +03:00
|
|
|
return new VersionMismatchError(payload);
|
2016-06-14 14:46:24 +03:00
|
|
|
} else if (this.isServerUnreachableError(status, headers, payload)) {
|
2017-11-04 01:59:39 +03:00
|
|
|
return new ServerUnreachableError(payload);
|
2016-06-30 17:45:02 +03:00
|
|
|
} else if (this.isRequestEntityTooLargeError(status, headers, payload)) {
|
2017-11-04 01:59:39 +03:00
|
|
|
return new RequestEntityTooLargeError(payload);
|
2016-05-22 11:20:02 +03:00
|
|
|
} else if (this.isUnsupportedMediaTypeError(status, headers, payload)) {
|
2017-11-04 01:59:39 +03:00
|
|
|
return new UnsupportedMediaTypeError(payload);
|
2016-07-08 16:54:36 +03:00
|
|
|
} else if (this.isMaintenanceError(status, headers, payload)) {
|
2017-11-04 01:59:39 +03:00
|
|
|
return new MaintenanceError(payload);
|
2016-08-24 21:22:20 +03:00
|
|
|
} else if (this.isThemeValidationError(status, headers, payload)) {
|
2017-11-04 01:59:39 +03:00
|
|
|
return new ThemeValidationError(payload);
|
2019-12-17 16:57:42 +03:00
|
|
|
} else if (this.isHostLimitError(status, headers, payload)) {
|
|
|
|
return new HostLimitError(payload);
|
2020-07-10 20:35:13 +03:00
|
|
|
} else if (this.isEmailError(status, headers, payload)) {
|
|
|
|
return new EmailError(payload);
|
2020-12-09 22:32:31 +03:00
|
|
|
} else if (this.isAcceptedResponse(status)) {
|
|
|
|
return new AcceptedResponse(payload);
|
2016-02-26 16:25:47 +03:00
|
|
|
}
|
|
|
|
|
2018-04-16 19:55:21 +03:00
|
|
|
let isGhostRequest = GHOST_REQUEST.test(request.url);
|
|
|
|
let isAuthenticated = this.get('session.isAuthenticated');
|
|
|
|
let isUnauthorized = this.isUnauthorizedError(status, headers, payload);
|
2021-07-14 14:27:51 +03:00
|
|
|
let isForbidden = isForbiddenError(status, headers, payload);
|
2018-04-16 19:55:21 +03:00
|
|
|
|
2021-06-30 16:51:40 +03:00
|
|
|
// used when reporting connection errors, helps distinguish CDN
|
|
|
|
if (isGhostRequest) {
|
|
|
|
this._responseServer = headers.server;
|
|
|
|
}
|
|
|
|
|
2021-07-14 14:27:51 +03:00
|
|
|
if (isAuthenticated && isGhostRequest && (isUnauthorized || (isForbidden && payload.errors?.[0].message === 'Authorization failed'))) {
|
2018-10-05 21:46:33 +03:00
|
|
|
this.skipSessionDeletion = true;
|
|
|
|
this.session.invalidate();
|
2016-09-26 19:59:04 +03:00
|
|
|
}
|
|
|
|
|
2022-02-03 01:11:11 +03:00
|
|
|
return super.handleResponse(...arguments);
|
|
|
|
}
|
2016-02-26 16:25:47 +03:00
|
|
|
|
2016-01-18 18:37:14 +03:00
|
|
|
normalizeErrorResponse(status, headers, payload) {
|
|
|
|
if (payload && typeof payload === 'object') {
|
2016-09-26 19:59:04 +03:00
|
|
|
let errors = payload.error || payload.errors || payload.message || undefined;
|
2016-09-26 16:07:18 +03:00
|
|
|
|
2016-09-26 19:59:04 +03:00
|
|
|
if (errors) {
|
|
|
|
if (!isEmberArray(errors)) {
|
|
|
|
errors = [errors];
|
2016-09-26 16:07:18 +03:00
|
|
|
}
|
2016-09-26 19:59:04 +03:00
|
|
|
|
2018-01-05 18:38:23 +03:00
|
|
|
payload.errors = errors.map(function (error) {
|
2016-09-26 19:59:04 +03:00
|
|
|
if (typeof error === 'string') {
|
|
|
|
return {message: error};
|
|
|
|
} else {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-01-18 18:37:14 +03:00
|
|
|
}
|
2016-04-04 13:44:54 +03:00
|
|
|
|
2022-02-03 01:11:11 +03:00
|
|
|
return super.normalizeErrorResponse(status, headers, payload);
|
|
|
|
}
|
2016-02-26 16:25:47 +03:00
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
isVersionMismatchError(status, headers, payload) {
|
|
|
|
return isVersionMismatchError(status, payload);
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2016-11-14 16:16:51 +03:00
|
|
|
isServerUnreachableError(status) {
|
2016-06-14 14:46:24 +03:00
|
|
|
return isServerUnreachableError(status);
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2016-06-14 14:46:24 +03:00
|
|
|
|
2016-11-14 16:16:51 +03:00
|
|
|
isRequestEntityTooLargeError(status) {
|
2016-05-22 11:20:02 +03:00
|
|
|
return isRequestEntityTooLargeError(status);
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2016-04-12 14:34:40 +03:00
|
|
|
|
2016-11-14 16:16:51 +03:00
|
|
|
isUnsupportedMediaTypeError(status) {
|
2016-05-22 11:20:02 +03:00
|
|
|
return isUnsupportedMediaTypeError(status);
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2016-07-08 16:54:36 +03:00
|
|
|
|
|
|
|
isMaintenanceError(status, headers, payload) {
|
|
|
|
return isMaintenanceError(status, payload);
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2016-08-24 21:22:20 +03:00
|
|
|
|
|
|
|
isThemeValidationError(status, headers, payload) {
|
|
|
|
return isThemeValidationError(status, payload);
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2019-12-17 16:57:42 +03:00
|
|
|
|
|
|
|
isHostLimitError(status, headers, payload) {
|
|
|
|
return isHostLimitError(status, payload);
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2020-07-10 20:35:13 +03:00
|
|
|
|
|
|
|
isEmailError(status, headers, payload) {
|
|
|
|
return isEmailError(status, payload);
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2020-12-09 22:32:31 +03:00
|
|
|
|
|
|
|
isAcceptedResponse(status) {
|
|
|
|
return isAcceptedResponse(status);
|
2016-01-18 18:37:14 +03:00
|
|
|
}
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2016-10-03 21:08:23 +03:00
|
|
|
|
|
|
|
// we need to reopen so that internal methods use the correct contentType
|
|
|
|
ajaxService.reopen({
|
|
|
|
contentType: 'application/json; charset=UTF-8'
|
|
|
|
});
|
|
|
|
|
|
|
|
export default ajaxService;
|