2021-12-07 21:40:46 +03:00
|
|
|
const _ = require('lodash');
|
2022-04-08 07:40:37 +03:00
|
|
|
const semver = require('semver');
|
2021-12-07 21:40:46 +03:00
|
|
|
const debug = require('@tryghost/debug')('error-handler');
|
|
|
|
const errors = require('@tryghost/errors');
|
2021-12-14 15:23:17 +03:00
|
|
|
const {prepareStackForUser} = require('@tryghost/errors').utils;
|
2022-09-20 12:47:59 +03:00
|
|
|
const {isReqResUserSpecific, cacheControlValues} = require('@tryghost/http-cache-utils');
|
2021-12-07 21:40:46 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
|
|
|
|
|
|
|
const messages = {
|
|
|
|
pageNotFound: 'Page not found',
|
|
|
|
resourceNotFound: 'Resource not found',
|
2022-04-13 08:29:57 +03:00
|
|
|
methodNotAcceptableVersionAhead: {
|
2022-05-04 15:04:40 +03:00
|
|
|
message: 'Request could not be served, the endpoint was not found.',
|
|
|
|
context: 'Provided client accept-version {acceptVersion} is ahead of current Ghost version {ghostVersion}.',
|
|
|
|
help: 'Try upgrading your Ghost install.'
|
2022-04-08 07:52:41 +03:00
|
|
|
},
|
2022-04-13 08:29:57 +03:00
|
|
|
methodNotAcceptableVersionBehind: {
|
2022-05-04 15:04:40 +03:00
|
|
|
message: 'Request could not be served, the endpoint was not found.',
|
|
|
|
context: 'Provided client accept-version {acceptVersion} is behind current Ghost version {ghostVersion}.',
|
|
|
|
help: 'Try upgrading your Ghost API client.'
|
2022-04-08 07:40:37 +03:00
|
|
|
},
|
2021-12-07 21:40:46 +03:00
|
|
|
actions: {
|
|
|
|
images: {
|
|
|
|
upload: 'upload image'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
userMessages: {
|
|
|
|
BookshelfRelationsError: 'Database error, cannot {action}.',
|
|
|
|
InternalServerError: 'Internal server error, cannot {action}.',
|
|
|
|
IncorrectUsageError: 'Incorrect usage error, cannot {action}.',
|
|
|
|
NotFoundError: 'Resource not found error, cannot {action}.',
|
|
|
|
BadRequestError: 'Request not understood error, cannot {action}.',
|
|
|
|
UnauthorizedError: 'Authorisation error, cannot {action}.',
|
|
|
|
NoPermissionError: 'Permission error, cannot {action}.',
|
|
|
|
ValidationError: 'Validation error, cannot {action}.',
|
|
|
|
UnsupportedMediaTypeError: 'Unsupported media error, cannot {action}.',
|
|
|
|
TooManyRequestsError: 'Too many requests error, cannot {action}.',
|
|
|
|
MaintenanceError: 'Server down for maintenance, cannot {action}.',
|
|
|
|
MethodNotAllowedError: 'Method not allowed, cannot {action}.',
|
|
|
|
RequestEntityTooLargeError: 'Request too large, cannot {action}.',
|
|
|
|
TokenRevocationError: 'Token is not available, cannot {action}.',
|
|
|
|
VersionMismatchError: 'Version mismatch error, cannot {action}.',
|
|
|
|
DataExportError: 'Error exporting content.',
|
|
|
|
DataImportError: 'Duplicated entry, cannot save {action}.',
|
|
|
|
DatabaseVersionError: 'Database version compatibility error, cannot {action}.',
|
|
|
|
EmailError: 'Error sending email!',
|
|
|
|
ThemeValidationError: 'Theme validation error, cannot {action}.',
|
|
|
|
HostLimitError: 'Host Limit error, cannot {action}.',
|
|
|
|
DisabledFeatureError: 'Theme validation error, the {{{helperName}}} helper is not available. Cannot {action}.',
|
|
|
|
UpdateCollisionError: 'Saving failed! Someone else is editing this post.'
|
2022-03-24 13:06:55 +03:00
|
|
|
},
|
|
|
|
UnknownError: 'Unknown error - {name}, cannot {action}.'
|
2021-12-07 21:40:46 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an error ready to be shown the the user
|
|
|
|
*/
|
|
|
|
module.exports.prepareError = (err, req, res, next) => {
|
|
|
|
debug(err);
|
|
|
|
|
|
|
|
if (Array.isArray(err)) {
|
|
|
|
err = err[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!errors.utils.isGhostError(err)) {
|
2022-08-24 10:28:20 +03:00
|
|
|
// We need a special case for 404 errors & bookshelf empty errors
|
|
|
|
if ((err.statusCode && err.statusCode === 404) || err.message === 'EmptyResponse') {
|
2021-12-07 21:40:46 +03:00
|
|
|
err = new errors.NotFoundError({
|
|
|
|
err: err
|
|
|
|
});
|
|
|
|
} else if (err.stack.match(/node_modules\/handlebars\//)) {
|
|
|
|
// Temporary handling of theme errors from handlebars
|
|
|
|
// @TODO remove this when #10496 is solved properly
|
|
|
|
err = new errors.IncorrectUsageError({
|
|
|
|
err: err,
|
|
|
|
message: err.message,
|
|
|
|
statusCode: err.statusCode
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
err = new errors.InternalServerError({
|
|
|
|
err: err,
|
|
|
|
message: err.message,
|
|
|
|
statusCode: err.statusCode
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// used for express logging middleware see core/server/app.js
|
|
|
|
req.err = err;
|
|
|
|
|
|
|
|
// alternative for res.status();
|
|
|
|
res.statusCode = err.statusCode;
|
|
|
|
|
|
|
|
next(err);
|
|
|
|
};
|
|
|
|
|
2022-03-21 12:33:54 +03:00
|
|
|
module.exports.prepareStack = (err, req, res, next) => { // eslint-disable-line no-unused-vars
|
|
|
|
const clonedError = prepareStackForUser(err);
|
|
|
|
|
|
|
|
next(clonedError);
|
|
|
|
};
|
|
|
|
|
2022-09-20 12:47:59 +03:00
|
|
|
/**
|
|
|
|
* @private the method is exposed for testing purposes only
|
|
|
|
* @param {Object} err
|
|
|
|
* @param {import('express').Request} req
|
|
|
|
* @param {import('express').Response} res
|
|
|
|
* @param {import('express').NextFunction} next
|
|
|
|
*/
|
|
|
|
module.exports.jsonErrorRenderer = (err, req, res, next) => { // eslint-disable-line no-unused-vars
|
2021-12-07 21:40:46 +03:00
|
|
|
const userError = prepareUserMessage(err, req);
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
errors: [{
|
2022-05-06 17:42:58 +03:00
|
|
|
message: userError.message,
|
2021-12-07 21:40:46 +03:00
|
|
|
context: userError.context || null,
|
|
|
|
type: err.errorType || null,
|
|
|
|
details: err.errorDetails || null,
|
|
|
|
property: err.property || null,
|
|
|
|
help: err.help || null,
|
|
|
|
code: err.code || null,
|
2022-05-06 17:42:58 +03:00
|
|
|
id: err.id || null,
|
|
|
|
ghostErrorCode: err.ghostErrorCode || null
|
2021-12-07 21:40:46 +03:00
|
|
|
}]
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-09-20 12:47:59 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {String} [cacheControlHeaderValue] cache-control header value
|
|
|
|
*/
|
|
|
|
module.exports.prepareErrorCacheControl = (cacheControlHeaderValue) => {
|
|
|
|
return (err, req, res, next) => {
|
|
|
|
let cacheControl = cacheControlHeaderValue;
|
|
|
|
if (!cacheControlHeaderValue) {
|
|
|
|
// never cache errors unless it's a 404
|
|
|
|
cacheControl = cacheControlValues.private;
|
2022-09-20 12:08:10 +03:00
|
|
|
|
2022-09-20 12:47:59 +03:00
|
|
|
// Do not include 'private' cache-control directive for 404 responses
|
|
|
|
if (err.statusCode === 404 && req.method === 'GET' && !isReqResUserSpecific(req, res)) {
|
|
|
|
cacheControl = cacheControlValues.noCacheDynamic;
|
|
|
|
}
|
|
|
|
}
|
2022-09-20 12:08:10 +03:00
|
|
|
|
2022-09-20 12:47:59 +03:00
|
|
|
res.set({
|
|
|
|
'Cache-Control': cacheControl
|
|
|
|
});
|
|
|
|
|
|
|
|
next(err);
|
|
|
|
};
|
2022-09-20 12:08:10 +03:00
|
|
|
};
|
|
|
|
|
2021-12-14 15:23:17 +03:00
|
|
|
const prepareUserMessage = (err, req) => {
|
2021-12-07 21:40:46 +03:00
|
|
|
const userError = {
|
|
|
|
message: err.message,
|
|
|
|
context: err.context
|
|
|
|
};
|
|
|
|
|
2021-12-14 15:23:17 +03:00
|
|
|
const docName = _.get(req, 'frameOptions.docName');
|
|
|
|
const method = _.get(req, 'frameOptions.method');
|
2021-12-07 21:40:46 +03:00
|
|
|
|
|
|
|
if (docName && method) {
|
|
|
|
let action;
|
|
|
|
|
|
|
|
const actionMap = {
|
|
|
|
browse: 'list',
|
|
|
|
read: 'read',
|
|
|
|
add: 'save',
|
|
|
|
edit: 'edit',
|
|
|
|
destroy: 'delete'
|
|
|
|
};
|
|
|
|
|
|
|
|
if (_.get(messages.actions, [docName, method])) {
|
|
|
|
action = tpl(messages.actions[docName][method]);
|
|
|
|
} else if (Object.keys(actionMap).includes(method)) {
|
|
|
|
let resource = docName;
|
|
|
|
|
|
|
|
if (method !== 'browse') {
|
|
|
|
resource = resource.replace(/s$/, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
action = `${actionMap[method]} ${resource}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action) {
|
|
|
|
if (err.context) {
|
|
|
|
userError.context = `${err.message} ${err.context}`;
|
|
|
|
} else {
|
|
|
|
userError.context = err.message;
|
|
|
|
}
|
|
|
|
|
2022-03-24 13:06:55 +03:00
|
|
|
if (_.get(messages.userMessages, err.name)) {
|
|
|
|
userError.message = tpl(messages.userMessages[err.name], {action: action});
|
|
|
|
} else {
|
|
|
|
userError.message = tpl(messages.UnknownError, {action, name: err.name});
|
|
|
|
}
|
2021-12-07 21:40:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return userError;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.resourceNotFound = (req, res, next) => {
|
2022-04-08 07:40:37 +03:00
|
|
|
if (req && req.headers && req.headers['accept-version']
|
|
|
|
&& res.locals && res.locals.safeVersion
|
2022-04-08 07:52:41 +03:00
|
|
|
&& semver.compare(semver.coerce(req.headers['accept-version']), semver.coerce(res.locals.safeVersion)) !== 0) {
|
|
|
|
const versionComparison = semver.compare(
|
|
|
|
semver.coerce(req.headers['accept-version']),
|
|
|
|
semver.coerce(res.locals.safeVersion)
|
|
|
|
);
|
|
|
|
|
2022-05-02 15:46:23 +03:00
|
|
|
let notAcceptableError;
|
2022-04-08 07:52:41 +03:00
|
|
|
if (versionComparison === 1) {
|
2022-05-02 15:46:23 +03:00
|
|
|
notAcceptableError = new errors.RequestNotAcceptableError({
|
2022-04-08 07:52:41 +03:00
|
|
|
message: tpl(
|
2022-04-13 08:29:57 +03:00
|
|
|
messages.methodNotAcceptableVersionAhead.message
|
2022-04-08 07:52:41 +03:00
|
|
|
),
|
2022-04-13 08:29:57 +03:00
|
|
|
context: tpl(messages.methodNotAcceptableVersionAhead.context, {
|
2022-04-08 07:52:41 +03:00
|
|
|
acceptVersion: req.headers['accept-version'],
|
|
|
|
ghostVersion: `v${res.locals.safeVersion}`
|
|
|
|
}),
|
2022-04-20 10:44:26 +03:00
|
|
|
help: tpl(messages.methodNotAcceptableVersionAhead.help),
|
|
|
|
code: 'UPDATE_GHOST'
|
2022-05-02 15:46:23 +03:00
|
|
|
});
|
2022-04-08 07:52:41 +03:00
|
|
|
} else {
|
2022-05-02 15:46:23 +03:00
|
|
|
notAcceptableError = new errors.RequestNotAcceptableError({
|
2022-04-08 07:52:41 +03:00
|
|
|
message: tpl(
|
2022-04-13 08:29:57 +03:00
|
|
|
messages.methodNotAcceptableVersionBehind.message
|
2022-04-08 07:52:41 +03:00
|
|
|
),
|
2022-04-13 08:29:57 +03:00
|
|
|
context: tpl(messages.methodNotAcceptableVersionBehind.context, {
|
2022-04-08 07:52:41 +03:00
|
|
|
acceptVersion: req.headers['accept-version'],
|
|
|
|
ghostVersion: `v${res.locals.safeVersion}`
|
|
|
|
}),
|
2022-04-20 10:44:26 +03:00
|
|
|
help: tpl(messages.methodNotAcceptableVersionBehind.help),
|
|
|
|
code: 'UPDATE_CLIENT'
|
2022-05-02 15:46:23 +03:00
|
|
|
});
|
2022-04-08 07:52:41 +03:00
|
|
|
}
|
|
|
|
|
2022-05-02 15:46:23 +03:00
|
|
|
next(notAcceptableError);
|
2022-04-08 07:40:37 +03:00
|
|
|
} else {
|
|
|
|
next(new errors.NotFoundError({message: tpl(messages.resourceNotFound)}));
|
|
|
|
}
|
2021-12-07 21:40:46 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.pageNotFound = (req, res, next) => {
|
|
|
|
next(new errors.NotFoundError({message: tpl(messages.pageNotFound)}));
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.handleJSONResponse = sentry => [
|
2022-03-11 13:47:26 +03:00
|
|
|
// Make sure the error can be served
|
|
|
|
module.exports.prepareError,
|
2022-09-20 12:08:10 +03:00
|
|
|
// Add cache-control header
|
2022-09-20 12:47:59 +03:00
|
|
|
module.exports.prepareErrorCacheControl(),
|
2022-03-21 12:33:54 +03:00
|
|
|
// Handle the error in Sentry
|
|
|
|
sentry.errorHandler,
|
|
|
|
// Format the stack for the user
|
|
|
|
module.exports.prepareStack,
|
2021-12-07 21:40:46 +03:00
|
|
|
// Render the error using JSON format
|
2022-09-20 12:47:59 +03:00
|
|
|
module.exports.jsonErrorRenderer
|
2021-12-07 21:40:46 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
module.exports.handleHTMLResponse = sentry => [
|
2022-03-21 12:33:54 +03:00
|
|
|
// Make sure the error can be served
|
|
|
|
module.exports.prepareError,
|
2022-09-20 12:47:59 +03:00
|
|
|
// Add cache-control header
|
|
|
|
module.exports.prepareErrorCacheControl(cacheControlValues.private),
|
2021-12-07 21:40:46 +03:00
|
|
|
// Handle the error in Sentry
|
2022-03-11 13:47:26 +03:00
|
|
|
sentry.errorHandler,
|
2022-03-21 12:33:54 +03:00
|
|
|
// Format the stack for the user
|
|
|
|
module.exports.prepareStack
|
2021-12-07 21:40:46 +03:00
|
|
|
];
|