Ghost/core/server/errorHandling.js

56 lines
1.2 KiB
JavaScript
Raw Normal View History

var _ = require('underscore'),
errors;
/**
* Basic error handling helpers
*/
errors = {
throwError: function (err) {
if (!err) {
err = new Error("An error occurred");
}
2013-05-26 22:51:58 +04:00
if (_.isString(err)) {
throw new Error(err);
}
2013-05-26 22:51:58 +04:00
throw err;
},
2013-05-26 22:51:58 +04:00
logError: function (err) {
err = err || "Unknown";
// TODO: Logging framework hookup
console.log("Error occurred: ", err.message || err, err.stack || "");
},
2013-05-26 22:51:58 +04:00
logAndThrowError: function (err) {
this.logError(err);
2013-05-26 22:51:58 +04:00
this.throwError(err);
},
2013-05-26 22:51:58 +04:00
logErrorWithMessage: function (msg) {
var self = this;
2013-05-26 22:51:58 +04:00
return function () {
self.logError(msg);
};
},
2013-05-26 22:51:58 +04:00
logErrorWithRedirect: function (msg, redirectTo, req, res) {
var self = this;
2013-05-26 22:51:58 +04:00
return function () {
self.logError(msg);
2013-05-26 22:51:58 +04:00
if (_.isFunction(res.redirect)) {
res.redirect(redirectTo);
}
};
}
};
2013-05-26 22:51:58 +04:00
// Ensure our 'this' context in the functions
_.bindAll(errors, "throwError", "logError", "logAndThrowError", "logErrorWithMessage", "logErrorWithRedirect");
2013-05-26 22:51:58 +04:00
module.exports = errors;