Improved error handling

fixes #845

- only returns an error page for get requests, otherwise returns a response
- no more admin menu when not logged in
- no more error message about theme error template
- logWarn is available
This commit is contained in:
Hannah Wolfe 2013-10-17 22:49:14 +01:00
parent f12a3cecf7
commit 158d237122
2 changed files with 42 additions and 20 deletions

View File

@ -302,10 +302,10 @@ when(ghost.init()).then(function () {
// ### Error handling // ### Error handling
// 404 Handler // 404 Handler
server.use(errors.render404Page); server.use(errors.error404);
// 500 Handler // 500 Handler
server.use(errors.render500Page); server.use(errors.error500);
// ## Routing // ## Routing

View File

@ -29,6 +29,26 @@ errors = {
throw err; throw err;
}, },
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('');
}
},
logError: function (err, context, help) { logError: function (err, context, help) {
var stack = err ? err.stack : null; var stack = err ? err.stack : null;
err = err.message || err || 'Unknown'; err = err.message || err || 'Unknown';
@ -41,7 +61,7 @@ errors = {
console.error('\nERROR:'.red, err.red); console.error('\nERROR:'.red, err.red);
if (context) { if (context) {
console.error(context); console.error(context.white);
} }
if (help) { if (help) {
@ -133,7 +153,7 @@ errors = {
} }
// Are we admin? If so, don't worry about the user template // Are we admin? If so, don't worry about the user template
if (res.isAdmin || userErrorTemplateExists === true) { if ((res.isAdmin && req.session.user) || userErrorTemplateExists === true) {
return renderErrorInt(); return renderErrorInt();
} }
@ -150,27 +170,29 @@ errors = {
return renderErrorInt(); return renderErrorInt();
} }
// Message only displays the first time an error is triggered.
errors.logError(
"Theme error template not found",
null,
"Add an error.hbs template to the theme for customised errors."
);
renderErrorInt(defaultErrorTemplatePath); renderErrorInt(defaultErrorTemplatePath);
}); });
}, },
render404Page: function (req, res, next) { error404: function (req, res, next) {
var message = res.isAdmin ? "No Ghost Found" : "Page Not Found"; var message = res.isAdmin && req.session.user ? "No Ghost Found" : "Page Not Found";
this.renderErrorPage(404, message, req, res, next);
if (req.method === 'GET') {
this.renderErrorPage(404, message, req, res, next);
} else {
res.send(404, message);
}
}, },
render500Page: function (err, req, res, next) { error500: function (err, req, res, next) {
if (!err || !(err instanceof Error)) { if (req.method === 'GET') {
next(); if (!err || !(err instanceof Error)) {
next();
}
errors.renderErrorPage(500, err, req, res, next);
} else {
res.send(500, err);
} }
errors.renderErrorPage(500, err, req, res, next);
} }
}; };
@ -182,8 +204,8 @@ _.bindAll(
'logAndThrowError', 'logAndThrowError',
'logErrorWithRedirect', 'logErrorWithRedirect',
'renderErrorPage', 'renderErrorPage',
'render404Page', 'error404',
'render500Page' 'error500'
); );
module.exports = errors; module.exports = errors;