Ghost/core/server/errorHandling.js
Hannah Wolfe 71a92194ca Improved error messaging
closes #748

- Removed the alpha software warning
- Better error message output for the whole app - can now specify an error, a context, and a help message
- Improved invalid node version, start and stop messaging
- Listens for Ctrl+C and exits nicely
- Minor improvements to handling and errors with old DBs (temporary)
2013-09-15 13:52:58 +01:00

70 lines
1.8 KiB
JavaScript

var _ = require('underscore'),
colors = require("colors"),
errors;
/**
* Basic error handling helpers
*/
errors = {
throwError: function (err) {
if (!err) {
err = new Error("An error occurred");
}
if (_.isString(err)) {
throw new Error(err);
}
throw err;
},
logError: function (err, context, help) {
err = err.message || err || "Unknown";
// TODO: Logging framework hookup
// Eventually we'll have better logging which will know about envs
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'staging'
|| process.env.NODE_ENV === 'production') {
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("");
}
},
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);
},
logAndThrowError: function (err, context, help) {
this.logError(err, context, help);
this.throwError(err, context, help);
},
logErrorWithRedirect: function (msg, context, help, redirectTo, req, res) {
var self = this;
return function () {
self.logError(msg, context, help);
if (_.isFunction(res.redirect)) {
res.redirect(redirectTo);
}
};
}
};
// Ensure our 'this' context in the functions
_.bindAll(errors, "throwError", "logError", "logAndThrowError", "logErrorWithRedirect");
module.exports = errors;