2013-09-19 06:50:06 +04:00
|
|
|
/*jslint regexp: true */
|
2013-09-24 14:46:30 +04:00
|
|
|
var _ = require('underscore'),
|
|
|
|
colors = require('colors'),
|
|
|
|
fs = require('fs'),
|
|
|
|
path = require('path'),
|
2013-09-17 14:56:05 +04:00
|
|
|
errors,
|
|
|
|
|
|
|
|
// Paths for views
|
2013-10-21 20:59:37 +04:00
|
|
|
appRoot = path.resolve(__dirname, '..', '..'),
|
|
|
|
themePath = path.resolve(appRoot, 'content', 'themes'),
|
|
|
|
adminTemplatePath = path.resolve(appRoot, 'core', 'server', 'views'),
|
|
|
|
defaultErrorTemplatePath = path.resolve(adminTemplatePath, 'user-error.hbs'),
|
|
|
|
userErrorTemplatePath = path.resolve(themePath, 'error.hbs'),
|
2013-09-17 14:56:05 +04:00
|
|
|
userErrorTemplateExists;
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Basic error handling helpers
|
|
|
|
*/
|
|
|
|
errors = {
|
2013-10-21 20:59:37 +04:00
|
|
|
updateActiveTheme: function (activeTheme) {
|
|
|
|
userErrorTemplatePath = path.resolve(themePath, activeTheme, 'error.hbs');
|
|
|
|
userErrorTemplateExists = undefined;
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
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')) {
|
|
|
|
|
|
|
|
console.log('\nWarning:'.yellow, warn.yellow);
|
|
|
|
|
|
|
|
if (context) {
|
|
|
|
console.log(context.white);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (help) {
|
|
|
|
console.log(help.green);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a new line
|
|
|
|
console.log('');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-09-15 15:11:47 +04:00
|
|
|
logError: function (err, context, help) {
|
2013-09-19 06:50:06 +04:00
|
|
|
var stack = err ? err.stack : null;
|
2013-09-24 14:46:30 +04:00
|
|
|
err = err.message || err || 'Unknown';
|
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
|
|
|
|
2013-09-24 14:46:30 +04:00
|
|
|
console.error('\nERROR:'.red, err.red);
|
2013-09-15 15:11:47 +04:00
|
|
|
|
|
|
|
if (context) {
|
2013-10-18 01:49:14 +04:00
|
|
|
console.error(context.white);
|
2013-09-15 15:11:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (help) {
|
2013-09-19 06:50:06 +04:00
|
|
|
console.error(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
|
2013-09-24 14:46:30 +04:00
|
|
|
console.error('');
|
2013-09-19 06:50:06 +04:00
|
|
|
|
|
|
|
if (stack) {
|
2013-09-24 14:46:30 +04:00
|
|
|
console.error(stack, '\n');
|
2013-09-19 06:50:06 +04:00
|
|
|
}
|
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) {
|
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) {
|
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-09-17 14:56:05 +04:00
|
|
|
// TODO: Attach node-polyglot
|
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
|
2013-09-17 14:56:05 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (code >= 500) {
|
2013-09-19 06:50:06 +04:00
|
|
|
this.logError(err, "ErrorPage", "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.
|
2013-09-17 14:56:05 +04:00
|
|
|
if (userErrorTemplateExists === false) {
|
2013-09-18 09:12:53 +04:00
|
|
|
return renderErrorInt(defaultErrorTemplatePath);
|
2013-09-17 14:56:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// userErrorTemplateExists is undefined, which means we
|
|
|
|
// haven't yet checked for it. Do so now!
|
|
|
|
fs.stat(userErrorTemplatePath, function (err, stat) {
|
|
|
|
userErrorTemplateExists = !err;
|
|
|
|
if (userErrorTemplateExists) {
|
|
|
|
return renderErrorInt();
|
|
|
|
}
|
|
|
|
|
2013-09-18 09:12:53 +04:00
|
|
|
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";
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if (req.method === 'GET') {
|
|
|
|
if (!err || !(err instanceof Error)) {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
errors.renderErrorPage(500, err, req, res, next);
|
|
|
|
} 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
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// Ensure our 'this' context in the functions
|
2013-09-17 14:56:05 +04:00
|
|
|
_.bindAll(
|
|
|
|
errors,
|
2013-09-24 14:46:30 +04:00
|
|
|
'throwError',
|
|
|
|
'logError',
|
|
|
|
'logAndThrowError',
|
|
|
|
'logErrorWithRedirect',
|
|
|
|
'renderErrorPage',
|
2013-10-18 01:49:14 +04:00
|
|
|
'error404',
|
|
|
|
'error500'
|
2013-09-17 14:56:05 +04:00
|
|
|
);
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-10-17 23:42:15 +04:00
|
|
|
module.exports = errors;
|