2013-09-19 06:50:06 +04:00
|
|
|
/*jslint regexp: true */
|
2014-05-09 14:11:29 +04:00
|
|
|
var _ = require('lodash'),
|
|
|
|
colors = require('colors'),
|
|
|
|
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'),
|
|
|
|
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'),
|
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-02-27 06:44:09 +04:00
|
|
|
// This is not useful but required for jshint
|
|
|
|
colors.setTheme({silly: 'rainbow'});
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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) {
|
2014-07-25 02:07:29 +04:00
|
|
|
err = new Error('An error occurred');
|
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
|
|
|
},
|
|
|
|
|
2014-07-10 19:10:00 +04:00
|
|
|
logInfo: function (component, info) {
|
|
|
|
if ((process.env.NODE_ENV === 'development' ||
|
|
|
|
process.env.NODE_ENV === 'staging' ||
|
|
|
|
process.env.NODE_ENV === 'production')) {
|
|
|
|
var msg = [component.cyan + ':'.cyan, info.cyan];
|
|
|
|
|
|
|
|
console.info.apply(console, msg);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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')) {
|
2014-09-21 01:11:30 +04:00
|
|
|
warn = warn || 'no message supplied';
|
2014-03-22 02:31:58 +04:00
|
|
|
var msgs = ['\nWarning:'.yellow, warn.yellow, '\n'];
|
2013-10-18 01:49:14 +04:00
|
|
|
|
|
|
|
if (context) {
|
2014-03-22 02:31:58 +04:00
|
|
|
msgs.push(context.white, '\n');
|
2013-10-18 01:49:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (help) {
|
2014-03-22 02:31:58 +04:00
|
|
|
msgs.push(help.green);
|
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 {
|
|
|
|
err = 'An unknown error occurred.';
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2014-09-10 08:06:24 +04:00
|
|
|
context = 'Your database is in read only mode. Visitors can read your blog, but you can\'t log in or add posts.';
|
|
|
|
help = 'Check your database file and make sure that file owner and permissions are correct.';
|
2014-08-08 13:16:51 +04: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
|
2013-09-19 06:50:06 +04:00
|
|
|
if ((process.env.NODE_ENV === 'development' ||
|
|
|
|
process.env.NODE_ENV === 'staging' ||
|
|
|
|
process.env.NODE_ENV === 'production')) {
|
2014-03-22 02:31:58 +04:00
|
|
|
msgs = ['\nERROR:'.red, err.red, '\n'];
|
2013-09-15 15:11:47 +04:00
|
|
|
|
|
|
|
if (context) {
|
2014-03-22 02:31:58 +04:00
|
|
|
msgs.push(context.white, '\n');
|
2013-09-15 15:11:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (help) {
|
2014-03-22 02:31:58 +04:00
|
|
|
msgs.push(help.green);
|
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
|
|
|
},
|
|
|
|
|
2014-07-31 04:15:34 +04:00
|
|
|
handleAPIError: function (error, permsMessage) {
|
2014-07-25 02:07:29 +04:00
|
|
|
if (!error) {
|
2014-07-31 04:15:34 +04:00
|
|
|
return this.rejectError(
|
|
|
|
new this.NoPermissionError(permsMessage || 'You do not have permission to perform this action')
|
|
|
|
);
|
2014-07-25 02:07:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_.isString(error)) {
|
|
|
|
return this.rejectError(new this.NoPermissionError(error));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error.type) {
|
|
|
|
return this.rejectError(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.rejectError(new this.InternalServerError(error));
|
|
|
|
},
|
|
|
|
|
2013-09-17 14:56:05 +04:00
|
|
|
renderErrorPage: function (code, 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;
|
|
|
|
|
2013-09-24 14:46:30 +04:00
|
|
|
if (process.env.NODE_ENV !== 'production' && err.stack) {
|
2013-09-19 06:50:06 +04:00
|
|
|
stack = parseStack(err.stack);
|
|
|
|
}
|
|
|
|
|
2013-10-17 23:42:15 +04:00
|
|
|
res.status(code).render((errorView || 'error'), {
|
2013-09-17 14:56:05 +04:00
|
|
|
message: err.message || err,
|
2013-09-19 06:50:06 +04:00
|
|
|
code: code,
|
|
|
|
stack: stack
|
2014-01-23 21:52:18 +04:00
|
|
|
}, function (templateErr, html) {
|
|
|
|
if (!templateErr) {
|
2014-08-09 17:48:58 +04:00
|
|
|
return res.status(code).send(html);
|
2014-01-23 21:52:18 +04:00
|
|
|
}
|
|
|
|
// There was an error trying to render the error page, output the error
|
|
|
|
self.logError(templateErr, 'Error whilst rendering error page', 'Error template has an error');
|
|
|
|
|
|
|
|
// 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(
|
2014-07-25 02:07:29 +04:00
|
|
|
'<h1>Oops, seems there is an an error in the error template.</h1>' +
|
|
|
|
'<p>Encountered the error: </p>' +
|
|
|
|
'<pre>' + hbs.handlebars.Utils.escapeExpression(templateErr.message || templateErr) + '</pre>' +
|
|
|
|
'<br ><p>whilst trying to render an error page for the error: </p>' +
|
|
|
|
code + ' ' + '<pre>' + hbs.handlebars.Utils.escapeExpression(err.message || err) + '</pre>'
|
|
|
|
);
|
2013-09-17 14:56:05 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (code >= 500) {
|
2014-07-25 02:07:29 +04:00
|
|
|
this.logError(err, 'Rendering Error Page', 'Ghost caught a processing error in the middleware layer.');
|
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) {
|
2014-07-25 02:07:29 +04:00
|
|
|
var message = res.isAdmin && req.user ? 'No Ghost Found' : 'Page Not Found';
|
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) {
|
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'});
|
|
|
|
|
|
|
|
if (err.status === 404) {
|
|
|
|
return this.error404(req, res, next);
|
|
|
|
}
|
|
|
|
|
2013-10-18 01:49:14 +04:00
|
|
|
if (req.method === 'GET') {
|
|
|
|
if (!err || !(err instanceof Error)) {
|
|
|
|
next();
|
|
|
|
}
|
2013-11-15 18:27:06 +04:00
|
|
|
errors.renderErrorPage(err.status || 500, err, req, res, next);
|
2013-10-18 01:49:14 +04:00
|
|
|
} else {
|
2014-07-17 16:22:07 +04:00
|
|
|
var statusCode = 500,
|
2014-08-08 11:44:24 +04:00
|
|
|
returnErrors = [];
|
2014-07-17 16:22:07 +04:00
|
|
|
|
2014-08-08 11:44:24 +04:00
|
|
|
if (!_.isArray(err)) {
|
|
|
|
err = [].concat(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
_.each(err, function (errorItem) {
|
|
|
|
var errorContent = {};
|
|
|
|
|
|
|
|
statusCode = errorItem.code || 500;
|
|
|
|
|
|
|
|
errorContent.message = _.isString(errorItem) ? errorItem :
|
|
|
|
(_.isObject(errorItem) ? errorItem.message : 'Unknown Error');
|
|
|
|
errorContent.type = errorItem.type || 'InternalServerError';
|
|
|
|
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',
|
|
|
|
'logInfo',
|
|
|
|
'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',
|
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;
|