2013-09-19 06:50:06 +04:00
|
|
|
/*jslint regexp: true */
|
2014-02-05 12:40:30 +04:00
|
|
|
var _ = require('lodash'),
|
2013-12-09 04:49:02 +04:00
|
|
|
colors = require('colors'),
|
2014-01-05 10:40:53 +04:00
|
|
|
config = require('./config'),
|
2013-12-09 04:49:02 +04:00
|
|
|
path = require('path'),
|
2014-01-03 19:50:03 +04:00
|
|
|
when = require('when'),
|
2014-01-23 21:52:18 +04:00
|
|
|
hbs = require('express-hbs'),
|
2013-09-17 14:56:05 +04:00
|
|
|
errors,
|
|
|
|
|
|
|
|
// Paths for views
|
2014-01-05 10:40:53 +04:00
|
|
|
defaultErrorTemplatePath = path.resolve(config().paths.adminViews, 'user-error.hbs'),
|
|
|
|
userErrorTemplatePath = path.resolve(config().paths.themePath, 'error.hbs'),
|
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'});
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
/**
|
|
|
|
* Basic error handling helpers
|
|
|
|
*/
|
|
|
|
errors = {
|
2014-01-03 00:57:37 +04:00
|
|
|
updateActiveTheme: function (activeTheme, hasErrorTemplate) {
|
2014-01-05 10:40:53 +04:00
|
|
|
userErrorTemplatePath = path.resolve(config().paths.themePath, activeTheme, 'error.hbs');
|
2014-01-03 00:57:37 +04:00
|
|
|
userErrorTemplateExists = hasErrorTemplate;
|
2013-10-21 20:59:37 +04:00
|
|
|
},
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
throwError: function (err) {
|
|
|
|
if (!err) {
|
|
|
|
err = new Error("An error occurred");
|
|
|
|
}
|
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) {
|
|
|
|
return when.reject(err);
|
|
|
|
},
|
|
|
|
|
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-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-03-22 02:31:58 +04:00
|
|
|
var stack = err ? err.stack : null,
|
|
|
|
msgs;
|
|
|
|
|
2014-01-04 00:07:08 +04:00
|
|
|
if (err) {
|
|
|
|
err = err.message || err || 'An unknown error occurred.';
|
|
|
|
} else {
|
|
|
|
err = 'An unknown error occurred.';
|
|
|
|
}
|
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')) {
|
2013-09-15 15:11:47 +04:00
|
|
|
|
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
|
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
renderErrorPage: function (code, err, req, res, next) {
|
2014-02-27 06:44:09 +04:00
|
|
|
/*jshint unused:false*/
|
2013-09-19 06:50:06 +04:00
|
|
|
|
2014-01-23 21:52:18 +04:00
|
|
|
var self = this;
|
|
|
|
|
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 {
|
2013-09-24 14:46:30 +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) {
|
|
|
|
return res.send(code, html);
|
|
|
|
}
|
|
|
|
// 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
|
|
|
|
return res.send(500, "<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-01-23 21:52:18 +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
|
2013-10-18 01:49:14 +04:00
|
|
|
if ((res.isAdmin && req.session.user) || 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) {
|
|
|
|
var message = res.isAdmin && req.session.user ? "No Ghost Found" : "Page Not Found";
|
|
|
|
|
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 {
|
|
|
|
res.send(404, message);
|
|
|
|
}
|
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 {
|
2013-10-21 09:53:26 +04:00
|
|
|
res.send(err.status || 500, err);
|
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([
|
2013-09-24 14:46:30 +04:00
|
|
|
'logAndThrowError',
|
|
|
|
'logErrorWithRedirect',
|
|
|
|
'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
|
|
|
|
2013-10-17 23:42:15 +04:00
|
|
|
module.exports = errors;
|