2013-06-25 15:43:15 +04:00
|
|
|
var _ = require('underscore'),
|
2013-09-15 15:11:47 +04:00
|
|
|
colors = require("colors"),
|
2013-09-17 14:56:05 +04:00
|
|
|
fs = require('fs'),
|
|
|
|
path = require('path'),
|
|
|
|
errors,
|
|
|
|
|
|
|
|
// Paths for views
|
|
|
|
appRoot = path.resolve(__dirname, '../'),
|
|
|
|
themePath = path.resolve(appRoot + '/content/themes'),
|
2013-09-18 09:12:53 +04:00
|
|
|
adminTemplatePath = path.resolve(appRoot + '/server/views/'),
|
|
|
|
defaultErrorTemplatePath = path.resolve(adminTemplatePath + '/user-error.hbs'),
|
2013-09-17 14:56:05 +04:00
|
|
|
userErrorTemplatePath = path.resolve(themePath + '/error.hbs'),
|
|
|
|
userErrorTemplateExists;
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Basic error handling helpers
|
|
|
|
*/
|
|
|
|
errors = {
|
|
|
|
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-09-15 15:11:47 +04:00
|
|
|
logError: function (err, context, help) {
|
|
|
|
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-07-16 22:57:19 +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
|
|
|
|
|
|
|
console.log("\nERROR:".red, err.red, err.stack || "");
|
|
|
|
|
|
|
|
if (context) {
|
|
|
|
console.log(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (help) {
|
|
|
|
console.log(help.green);
|
|
|
|
}
|
|
|
|
// add a new line
|
|
|
|
console.log("");
|
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) {
|
|
|
|
// Render the error!
|
2013-09-18 09:12:53 +04:00
|
|
|
function renderErrorInt(errorView) {
|
2013-09-17 14:56:05 +04:00
|
|
|
// TODO: Attach node-polyglot
|
2013-09-18 09:12:53 +04:00
|
|
|
res.render((errorView || "error"), {
|
2013-09-17 14:56:05 +04:00
|
|
|
message: err.message || err,
|
|
|
|
code: code
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (code >= 500) {
|
|
|
|
this.logError(err, "ErrorPage");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Are we admin? If so, don't worry about the user template
|
2013-09-18 09:12:53 +04:00
|
|
|
if (res.isAdmin || 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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."
|
|
|
|
);
|
|
|
|
|
2013-09-18 09:12:53 +04:00
|
|
|
renderErrorInt(defaultErrorTemplatePath);
|
2013-09-17 14:56:05 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
render404Page: function (req, res, next) {
|
2013-09-18 09:12:53 +04:00
|
|
|
var message = res.isAdmin ? "No Ghost Found" : "Page Not Found";
|
2013-09-17 14:56:05 +04:00
|
|
|
this.renderErrorPage(404, message, req, res, next);
|
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,
|
|
|
|
"throwError",
|
|
|
|
"logError",
|
|
|
|
"logAndThrowError",
|
|
|
|
"logErrorWithRedirect",
|
|
|
|
"renderErrorPage",
|
|
|
|
"render404Page"
|
|
|
|
);
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
module.exports = errors;
|