2015-05-28 18:16:09 +03:00
|
|
|
// # Errors
|
2013-09-19 06:50:06 +04:00
|
|
|
/*jslint regexp: true */
|
2014-05-09 14:11:29 +04:00
|
|
|
var _ = require('lodash'),
|
2015-05-19 18:51:53 +03:00
|
|
|
chalk = require('chalk'),
|
2014-05-09 14:11:29 +04:00
|
|
|
path = require('path'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2014-05-09 14:11:29 +04:00
|
|
|
hbs = require('express-hbs'),
|
2014-09-19 20:17:58 +04:00
|
|
|
NotFoundError = require('./not-found-error'),
|
|
|
|
BadRequestError = require('./bad-request-error'),
|
|
|
|
InternalServerError = require('./internal-server-error'),
|
|
|
|
NoPermissionError = require('./no-permission-error'),
|
2014-09-23 12:23:10 +04:00
|
|
|
MethodNotAllowedError = require('./method-not-allowed-error'),
|
2014-09-19 20:17:58 +04:00
|
|
|
RequestEntityTooLargeError = require('./request-too-large-error'),
|
|
|
|
UnauthorizedError = require('./unauthorized-error'),
|
|
|
|
ValidationError = require('./validation-error'),
|
|
|
|
UnsupportedMediaTypeError = require('./unsupported-media-type-error'),
|
|
|
|
EmailError = require('./email-error'),
|
|
|
|
DataImportError = require('./data-import-error'),
|
2015-08-10 14:26:39 +03:00
|
|
|
TooManyRequestsError = require('./too-many-requests-error'),
|
2016-03-06 21:18:33 +03:00
|
|
|
TokenRevocationError = require('./token-revocation-error'),
|
2016-06-09 18:14:36 +03:00
|
|
|
VersionMismatchError = require('./version-mismatch-error'),
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n = require('../i18n'),
|
2014-08-23 20:19:13 +04:00
|
|
|
config,
|
2013-09-17 14:56:05 +04:00
|
|
|
errors,
|
|
|
|
|
|
|
|
// Paths for views
|
2014-03-05 07:22:00 +04:00
|
|
|
userErrorTemplateExists = false;
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2014-08-23 20:19:13 +04:00
|
|
|
// Shim right now to deal with circular dependencies.
|
2014-09-22 00:54:32 +04:00
|
|
|
// @TODO(hswolff): remove circular dependency and lazy require.
|
2014-08-23 20:19:13 +04:00
|
|
|
function getConfigModule() {
|
|
|
|
if (!config) {
|
|
|
|
config = require('../config');
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2016-02-17 17:51:57 +03:00
|
|
|
function isValidErrorStatus(status) {
|
|
|
|
return _.isNumber(status) && status >= 400 && status < 600;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getStatusCode(error) {
|
|
|
|
if (error.statusCode) {
|
|
|
|
return error.statusCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error.status && isValidErrorStatus(error.status)) {
|
|
|
|
error.statusCode = error.status;
|
|
|
|
return error.statusCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error.code && isValidErrorStatus(error.code)) {
|
|
|
|
error.statusCode = error.code;
|
|
|
|
return error.statusCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
error.statusCode = 500;
|
|
|
|
return error.statusCode;
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
/**
|
|
|
|
* Basic error handling helpers
|
|
|
|
*/
|
|
|
|
errors = {
|
2014-03-27 00:43:16 +04:00
|
|
|
updateActiveTheme: function (activeTheme) {
|
2014-08-23 20:19:13 +04:00
|
|
|
userErrorTemplateExists = getConfigModule().paths.availableThemes[activeTheme].hasOwnProperty('error.hbs');
|
2013-10-21 20:59:37 +04:00
|
|
|
},
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
throwError: function (err) {
|
|
|
|
if (!err) {
|
2015-11-12 15:29:45 +03:00
|
|
|
err = new Error(i18n.t('errors.errors.anErrorOccurred'));
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
if (_.isString(err)) {
|
|
|
|
throw new Error(err);
|
|
|
|
}
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
throw err;
|
|
|
|
},
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2014-01-03 19:50:03 +04:00
|
|
|
// ## Reject Error
|
|
|
|
// Used to pass through promise errors when we want to handle them at a later time
|
|
|
|
rejectError: function (err) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(err);
|
2014-01-03 19:50:03 +04:00
|
|
|
},
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
logComponentInfo: function (component, info) {
|
2014-07-10 19:10:00 +04:00
|
|
|
if ((process.env.NODE_ENV === 'development' ||
|
|
|
|
process.env.NODE_ENV === 'staging' ||
|
|
|
|
process.env.NODE_ENV === 'production')) {
|
2015-05-19 18:51:53 +03:00
|
|
|
console.info(chalk.cyan(component + ':', info));
|
2014-07-10 19:10:00 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
logComponentWarn: function (component, warning) {
|
|
|
|
if ((process.env.NODE_ENV === 'development' ||
|
|
|
|
process.env.NODE_ENV === 'staging' ||
|
|
|
|
process.env.NODE_ENV === 'production')) {
|
|
|
|
console.info(chalk.yellow(component + ':', warning));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-10-18 01:49:14 +04:00
|
|
|
logWarn: function (warn, context, help) {
|
|
|
|
if ((process.env.NODE_ENV === 'development' ||
|
|
|
|
process.env.NODE_ENV === 'staging' ||
|
|
|
|
process.env.NODE_ENV === 'production')) {
|
2015-11-12 15:29:45 +03:00
|
|
|
warn = warn || i18n.t('errors.errors.noMessageSupplied');
|
|
|
|
var msgs = [chalk.yellow(i18n.t('errors.errors.warning'), warn), '\n'];
|
2013-10-18 01:49:14 +04:00
|
|
|
|
|
|
|
if (context) {
|
2015-05-19 18:51:53 +03:00
|
|
|
msgs.push(chalk.white(context), '\n');
|
2013-10-18 01:49:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (help) {
|
2015-05-19 18:51:53 +03:00
|
|
|
msgs.push(chalk.green(help));
|
2013-10-18 01:49:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// add a new line
|
2014-03-22 02:31:58 +04:00
|
|
|
msgs.push('\n');
|
|
|
|
|
|
|
|
console.log.apply(console, msgs);
|
2013-10-18 01:49:14 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-09-15 15:11:47 +04:00
|
|
|
logError: function (err, context, help) {
|
2014-08-07 21:50:23 +04:00
|
|
|
var self = this,
|
|
|
|
origArgs = _.toArray(arguments).slice(1),
|
|
|
|
stack,
|
|
|
|
msgs;
|
|
|
|
|
2014-07-15 15:03:12 +04:00
|
|
|
if (_.isArray(err)) {
|
|
|
|
_.each(err, function (e) {
|
2014-08-07 21:50:23 +04:00
|
|
|
var newArgs = [e].concat(origArgs);
|
|
|
|
errors.logError.apply(self, newArgs);
|
2014-07-15 15:03:12 +04:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-07 21:50:23 +04:00
|
|
|
stack = err ? err.stack : null;
|
2014-03-22 02:31:58 +04:00
|
|
|
|
2014-11-05 06:03:33 +03:00
|
|
|
if (!_.isString(err)) {
|
|
|
|
if (_.isObject(err) && _.isString(err.message)) {
|
|
|
|
err = err.message;
|
|
|
|
} else {
|
2015-11-12 15:29:45 +03:00
|
|
|
err = i18n.t('errors.errors.unknownErrorOccurred');
|
2014-11-05 06:03:33 +03:00
|
|
|
}
|
|
|
|
}
|
2014-09-10 08:06:24 +04:00
|
|
|
|
2014-08-08 13:16:51 +04:00
|
|
|
// Overwrite error to provide information that this is probably a permission problem
|
|
|
|
// TODO: https://github.com/TryGhost/Ghost/issues/3687
|
|
|
|
if (err.indexOf('SQLITE_READONLY') !== -1) {
|
2015-11-12 15:29:45 +03:00
|
|
|
context = i18n.t('errors.errors.databaseIsReadOnly');
|
|
|
|
help = i18n.t('errors.errors.checkDatabase');
|
2014-08-08 13:16:51 +04:00
|
|
|
}
|
2016-05-08 17:49:12 +03:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// TODO: Logging framework hookup
|
2013-09-17 14:56:05 +04:00
|
|
|
// Eventually we'll have better logging which will know about envs
|
2016-05-25 10:34:46 +03:00
|
|
|
// you can use DEBUG=true when running tests and need error stdout
|
|
|
|
if ((process.env.NODE_LEVEL === 'DEBUG' ||
|
|
|
|
process.env.NODE_ENV === 'development' ||
|
2013-09-19 06:50:06 +04:00
|
|
|
process.env.NODE_ENV === 'staging' ||
|
|
|
|
process.env.NODE_ENV === 'production')) {
|
2015-11-12 15:29:45 +03:00
|
|
|
msgs = [chalk.red(i18n.t('errors.errors.error'), err), '\n'];
|
2013-09-15 15:11:47 +04:00
|
|
|
|
|
|
|
if (context) {
|
2015-05-19 18:51:53 +03:00
|
|
|
msgs.push(chalk.white(context), '\n');
|
2013-09-15 15:11:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (help) {
|
2015-05-19 18:51:53 +03:00
|
|
|
msgs.push(chalk.green(help));
|
2013-09-15 15:11:47 +04:00
|
|
|
}
|
2013-09-19 06:50:06 +04:00
|
|
|
|
2013-09-15 15:11:47 +04:00
|
|
|
// add a new line
|
2014-03-22 02:31:58 +04:00
|
|
|
msgs.push('\n');
|
2013-09-19 06:50:06 +04:00
|
|
|
|
|
|
|
if (stack) {
|
2014-03-22 02:31:58 +04:00
|
|
|
msgs.push(stack, '\n');
|
2013-09-19 06:50:06 +04:00
|
|
|
}
|
2014-03-22 02:31:58 +04:00
|
|
|
|
|
|
|
console.error.apply(console, msgs);
|
2013-07-16 22:57:19 +04:00
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-09-15 15:11:47 +04:00
|
|
|
logErrorAndExit: function (err, context, help) {
|
|
|
|
this.logError(err, context, help);
|
|
|
|
// Exit with 0 to prevent npm errors as we have our own
|
|
|
|
process.exit(0);
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-09-15 15:11:47 +04:00
|
|
|
logAndThrowError: function (err, context, help) {
|
|
|
|
this.logError(err, context, help);
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-09-15 15:11:47 +04:00
|
|
|
this.throwError(err, context, help);
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2014-07-15 15:03:12 +04:00
|
|
|
logAndRejectError: function (err, context, help) {
|
|
|
|
this.logError(err, context, help);
|
|
|
|
|
2014-07-25 02:07:29 +04:00
|
|
|
return this.rejectError(err, context, help);
|
2014-07-15 15:03:12 +04:00
|
|
|
},
|
|
|
|
|
2013-09-15 15:11:47 +04:00
|
|
|
logErrorWithRedirect: function (msg, context, help, redirectTo, req, res) {
|
2014-02-27 06:44:09 +04:00
|
|
|
/*jshint unused:false*/
|
2013-06-25 15:43:15 +04:00
|
|
|
var self = this;
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
return function () {
|
2013-09-15 15:11:47 +04:00
|
|
|
self.logError(msg, context, help);
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
if (_.isFunction(res.redirect)) {
|
|
|
|
res.redirect(redirectTo);
|
|
|
|
}
|
|
|
|
};
|
2013-09-17 14:56:05 +04:00
|
|
|
},
|
|
|
|
|
2015-06-14 22:07:52 +03:00
|
|
|
/**
|
|
|
|
* ### Format HTTP Errors
|
|
|
|
* Converts the error response from the API into a format which can be returned over HTTP
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Array} error
|
|
|
|
* @return {{errors: Array, statusCode: number}}
|
|
|
|
*/
|
|
|
|
formatHttpErrors: function formatHttpErrors(error) {
|
|
|
|
var statusCode = 500,
|
|
|
|
errors = [];
|
|
|
|
|
|
|
|
if (!_.isArray(error)) {
|
|
|
|
error = [].concat(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
_.each(error, function each(errorItem) {
|
|
|
|
var errorContent = {};
|
|
|
|
|
|
|
|
// TODO: add logic to set the correct status code
|
2016-02-17 17:51:57 +03:00
|
|
|
statusCode = getStatusCode(errorItem);
|
2015-06-14 22:07:52 +03:00
|
|
|
|
|
|
|
errorContent.message = _.isString(errorItem) ? errorItem :
|
2015-11-12 15:29:45 +03:00
|
|
|
(_.isObject(errorItem) ? errorItem.message : i18n.t('errors.errors.unknownApiError'));
|
2015-06-14 22:07:52 +03:00
|
|
|
errorContent.errorType = errorItem.errorType || 'InternalServerError';
|
|
|
|
errors.push(errorContent);
|
|
|
|
});
|
|
|
|
|
|
|
|
return {errors: errors, statusCode: statusCode};
|
|
|
|
},
|
|
|
|
|
2015-10-22 16:28:47 +03:00
|
|
|
formatAndRejectAPIError: function (error, permsMessage) {
|
2014-07-25 02:07:29 +04:00
|
|
|
if (!error) {
|
2014-07-31 04:15:34 +04:00
|
|
|
return this.rejectError(
|
2015-11-12 15:29:45 +03:00
|
|
|
new this.NoPermissionError(permsMessage || i18n.t('errors.errors.notEnoughPermission'))
|
2014-07-31 04:15:34 +04:00
|
|
|
);
|
2014-07-25 02:07:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_.isString(error)) {
|
|
|
|
return this.rejectError(new this.NoPermissionError(error));
|
|
|
|
}
|
|
|
|
|
2015-04-22 23:29:45 +03:00
|
|
|
if (error.errorType) {
|
2014-07-25 02:07:29 +04:00
|
|
|
return this.rejectError(error);
|
|
|
|
}
|
|
|
|
|
2015-01-21 21:01:19 +03:00
|
|
|
// handle database errors
|
|
|
|
if (error.code && (error.errno || error.detail)) {
|
|
|
|
error.db_error_code = error.code;
|
2015-04-22 23:29:45 +03:00
|
|
|
error.errorType = 'DatabaseError';
|
2016-02-17 17:51:57 +03:00
|
|
|
error.statusCode = 500;
|
2015-01-21 21:01:19 +03:00
|
|
|
|
|
|
|
return this.rejectError(error);
|
|
|
|
}
|
|
|
|
|
2014-07-25 02:07:29 +04:00
|
|
|
return this.rejectError(new this.InternalServerError(error));
|
|
|
|
},
|
|
|
|
|
2015-10-22 16:28:47 +03:00
|
|
|
handleAPIError: function errorHandler(err, req, res, next) {
|
|
|
|
/*jshint unused:false */
|
|
|
|
var httpErrors = this.formatHttpErrors(err);
|
|
|
|
this.logError(err);
|
|
|
|
// Send a properly formatted HTTP response containing the errors
|
|
|
|
res.status(httpErrors.statusCode).json({errors: httpErrors.errors});
|
|
|
|
},
|
|
|
|
|
2016-02-17 17:51:57 +03:00
|
|
|
renderErrorPage: function (statusCode, err, req, res, next) {
|
2014-02-27 06:44:09 +04:00
|
|
|
/*jshint unused:false*/
|
2014-08-23 20:19:13 +04:00
|
|
|
var self = this,
|
|
|
|
defaultErrorTemplatePath = path.resolve(getConfigModule().paths.adminViews, 'user-error.hbs');
|
2014-01-23 21:52:18 +04:00
|
|
|
|
2013-09-19 06:50:06 +04:00
|
|
|
function parseStack(stack) {
|
2013-10-25 23:31:53 +04:00
|
|
|
if (!_.isString(stack)) {
|
2013-09-19 06:50:06 +04:00
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: split out line numbers
|
|
|
|
var stackRegex = /\s*at\s*(\w+)?\s*\(([^\)]+)\)\s*/i;
|
|
|
|
|
|
|
|
return (
|
|
|
|
stack
|
|
|
|
.split(/[\r\n]+/)
|
|
|
|
.slice(1)
|
|
|
|
.map(function (line) {
|
|
|
|
var parts = line.match(stackRegex);
|
|
|
|
if (!parts) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2014-09-10 08:06:24 +04:00
|
|
|
function: parts[1],
|
|
|
|
at: parts[2]
|
2013-09-19 06:50:06 +04:00
|
|
|
};
|
|
|
|
})
|
|
|
|
.filter(function (line) {
|
|
|
|
return !!line;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-09-17 14:56:05 +04:00
|
|
|
// Render the error!
|
2013-09-18 09:12:53 +04:00
|
|
|
function renderErrorInt(errorView) {
|
2013-09-19 06:50:06 +04:00
|
|
|
var stack = null;
|
|
|
|
|
2016-02-17 17:51:57 +03:00
|
|
|
if (statusCode !== 404 && process.env.NODE_ENV !== 'production' && err.stack) {
|
2013-09-19 06:50:06 +04:00
|
|
|
stack = parseStack(err.stack);
|
|
|
|
}
|
|
|
|
|
2016-02-17 17:51:57 +03:00
|
|
|
res.status(statusCode).render((errorView || 'error'), {
|
2013-09-17 14:56:05 +04:00
|
|
|
message: err.message || err,
|
2016-02-17 17:51:57 +03:00
|
|
|
// We have to use code here, as it's the variable passed to the template
|
|
|
|
// And error templates can be customised... therefore this constitutes API
|
|
|
|
// In future I recommend we make this be used for a combo-version of statusCode & errorCode
|
|
|
|
code: statusCode,
|
|
|
|
// Adding this as being distinctly, the status code, as opposed to any other code see #6526
|
|
|
|
statusCode: statusCode,
|
2013-09-19 06:50:06 +04:00
|
|
|
stack: stack
|
2014-01-23 21:52:18 +04:00
|
|
|
}, function (templateErr, html) {
|
|
|
|
if (!templateErr) {
|
2016-02-17 17:51:57 +03:00
|
|
|
return res.status(statusCode).send(html);
|
2014-01-23 21:52:18 +04:00
|
|
|
}
|
|
|
|
// There was an error trying to render the error page, output the error
|
2015-11-12 15:29:45 +03:00
|
|
|
self.logError(templateErr, i18n.t('errors.errors.errorWhilstRenderingError'), i18n.t('errors.errors.errorTemplateHasError'));
|
2014-01-23 21:52:18 +04:00
|
|
|
|
|
|
|
// And then try to explain things to the user...
|
|
|
|
// Cheat and output the error using handlebars escapeExpression
|
2014-08-09 17:48:58 +04:00
|
|
|
return res.status(500).send(
|
2015-11-12 15:29:45 +03:00
|
|
|
'<h1>' + i18n.t('errors.errors.oopsErrorTemplateHasError') + '</h1>' +
|
|
|
|
'<p>' + i18n.t('errors.errors.encounteredError') + '</p>' +
|
2014-07-25 02:07:29 +04:00
|
|
|
'<pre>' + hbs.handlebars.Utils.escapeExpression(templateErr.message || templateErr) + '</pre>' +
|
2015-11-12 15:29:45 +03:00
|
|
|
'<br ><p>' + i18n.t('errors.errors.whilstTryingToRender') + '</p>' +
|
2016-02-17 17:51:57 +03:00
|
|
|
statusCode + ' ' + '<pre>' + hbs.handlebars.Utils.escapeExpression(err.message || err) + '</pre>'
|
2014-07-25 02:07:29 +04:00
|
|
|
);
|
2013-09-17 14:56:05 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-17 17:51:57 +03:00
|
|
|
if (statusCode >= 500) {
|
2015-11-12 15:29:45 +03:00
|
|
|
this.logError(err, i18n.t('errors.errors.renderingErrorPage'), i18n.t('errors.errors.caughtProcessingError'));
|
2013-09-17 14:56:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Are we admin? If so, don't worry about the user template
|
2014-06-30 16:58:10 +04:00
|
|
|
if ((res.isAdmin && req.user && req.user.id) || userErrorTemplateExists === true) {
|
2013-09-17 14:56:05 +04:00
|
|
|
return renderErrorInt();
|
|
|
|
}
|
|
|
|
|
2013-09-18 09:12:53 +04:00
|
|
|
// We're not admin and the template doesn't exist. Render the default.
|
2014-01-03 00:57:37 +04:00
|
|
|
return renderErrorInt(defaultErrorTemplatePath);
|
2013-09-17 14:56:05 +04:00
|
|
|
},
|
|
|
|
|
2013-10-18 01:49:14 +04:00
|
|
|
error404: function (req, res, next) {
|
2015-11-12 15:29:45 +03:00
|
|
|
var message = i18n.t('errors.errors.pageNotFound');
|
2013-10-18 01:49:14 +04:00
|
|
|
|
2014-03-05 07:22:00 +04:00
|
|
|
// do not cache 404 error
|
|
|
|
res.set({'Cache-Control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'});
|
2013-10-18 01:49:14 +04:00
|
|
|
if (req.method === 'GET') {
|
|
|
|
this.renderErrorPage(404, message, req, res, next);
|
|
|
|
} else {
|
2014-08-09 17:48:58 +04:00
|
|
|
res.status(404).send(message);
|
2013-10-18 01:49:14 +04:00
|
|
|
}
|
2013-09-19 06:50:06 +04:00
|
|
|
},
|
|
|
|
|
2013-10-18 01:49:14 +04:00
|
|
|
error500: function (err, req, res, next) {
|
2016-02-17 17:51:57 +03:00
|
|
|
var statusCode = getStatusCode(err),
|
|
|
|
returnErrors = [];
|
|
|
|
|
2013-12-31 03:13:25 +04:00
|
|
|
// 500 errors should never be cached
|
|
|
|
res.set({'Cache-Control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'});
|
|
|
|
|
2016-02-17 17:51:57 +03:00
|
|
|
if (statusCode === 404) {
|
2013-12-31 03:13:25 +04:00
|
|
|
return this.error404(req, res, next);
|
|
|
|
}
|
|
|
|
|
2013-10-18 01:49:14 +04:00
|
|
|
if (req.method === 'GET') {
|
|
|
|
if (!err || !(err instanceof Error)) {
|
|
|
|
next();
|
|
|
|
}
|
2016-02-17 17:51:57 +03:00
|
|
|
errors.renderErrorPage(statusCode, err, req, res, next);
|
2013-10-18 01:49:14 +04:00
|
|
|
} else {
|
2014-08-08 11:44:24 +04:00
|
|
|
if (!_.isArray(err)) {
|
|
|
|
err = [].concat(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
_.each(err, function (errorItem) {
|
|
|
|
var errorContent = {};
|
|
|
|
|
|
|
|
errorContent.message = _.isString(errorItem) ? errorItem :
|
2015-11-12 15:29:45 +03:00
|
|
|
(_.isObject(errorItem) ? errorItem.message : i18n.t('errors.errors.unknownError'));
|
2015-04-22 23:29:45 +03:00
|
|
|
errorContent.errorType = errorItem.errorType || 'InternalServerError';
|
2014-08-08 11:44:24 +04:00
|
|
|
returnErrors.push(errorContent);
|
|
|
|
});
|
2014-07-17 16:22:07 +04:00
|
|
|
|
2014-08-09 17:48:58 +04:00
|
|
|
res.status(statusCode).json({errors: returnErrors});
|
2013-09-19 06:50:06 +04:00
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
|
|
|
};
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2014-02-05 12:40:30 +04:00
|
|
|
// Ensure our 'this' context for methods and preserve method arity by
|
|
|
|
// using Function#bind for expressjs
|
|
|
|
_.each([
|
2014-07-25 02:07:29 +04:00
|
|
|
'logWarn',
|
2016-03-21 15:44:23 +03:00
|
|
|
'logComponentInfo',
|
|
|
|
'logComponentWarn',
|
2014-07-25 02:07:29 +04:00
|
|
|
'rejectError',
|
|
|
|
'throwError',
|
|
|
|
'logError',
|
2013-09-24 14:46:30 +04:00
|
|
|
'logAndThrowError',
|
2014-07-25 02:07:29 +04:00
|
|
|
'logAndRejectError',
|
|
|
|
'logErrorAndExit',
|
2013-09-24 14:46:30 +04:00
|
|
|
'logErrorWithRedirect',
|
2014-07-25 02:07:29 +04:00
|
|
|
'handleAPIError',
|
2015-10-22 16:28:47 +03:00
|
|
|
'formatAndRejectAPIError',
|
2015-06-14 22:07:52 +03:00
|
|
|
'formatHttpErrors',
|
2013-09-24 14:46:30 +04:00
|
|
|
'renderErrorPage',
|
2013-10-18 01:49:14 +04:00
|
|
|
'error404',
|
|
|
|
'error500'
|
2014-02-05 12:40:30 +04:00
|
|
|
], function (funcName) {
|
|
|
|
errors[funcName] = errors[funcName].bind(errors);
|
|
|
|
});
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2014-05-09 14:11:29 +04:00
|
|
|
module.exports = errors;
|
|
|
|
module.exports.NotFoundError = NotFoundError;
|
|
|
|
module.exports.BadRequestError = BadRequestError;
|
|
|
|
module.exports.InternalServerError = InternalServerError;
|
|
|
|
module.exports.NoPermissionError = NoPermissionError;
|
|
|
|
module.exports.UnauthorizedError = UnauthorizedError;
|
|
|
|
module.exports.ValidationError = ValidationError;
|
|
|
|
module.exports.RequestEntityTooLargeError = RequestEntityTooLargeError;
|
2014-05-24 10:05:12 +04:00
|
|
|
module.exports.UnsupportedMediaTypeError = UnsupportedMediaTypeError;
|
2014-05-09 03:12:18 +04:00
|
|
|
module.exports.EmailError = EmailError;
|
2014-07-29 01:41:45 +04:00
|
|
|
module.exports.DataImportError = DataImportError;
|
2014-09-23 12:23:10 +04:00
|
|
|
module.exports.MethodNotAllowedError = MethodNotAllowedError;
|
2015-08-10 14:26:39 +03:00
|
|
|
module.exports.TooManyRequestsError = TooManyRequestsError;
|
2016-03-06 21:18:33 +03:00
|
|
|
module.exports.TokenRevocationError = TokenRevocationError;
|
2016-06-09 18:14:36 +03:00
|
|
|
module.exports.VersionMismatchError = VersionMismatchError;
|