Ghost/core/server/errorHandling.js
cobbspur 115996b88d improves unit testing for pagination and fixes pagination partial template (removed extra </nav>)
removes console logs for error handling in testing and travis environments
2013-07-16 19:57:19 +01:00

60 lines
1.4 KiB
JavaScript

var _ = require('underscore'),
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) {
err = 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("Error occurred: ", err.message || err, err.stack || "");
}
},
logAndThrowError: function (err) {
this.logError(err);
this.throwError(err);
},
logErrorWithMessage: function (msg) {
var self = this;
return function () {
self.logError(msg);
};
},
logErrorWithRedirect: function (msg, redirectTo, req, res) {
var self = this;
return function () {
self.logError(msg);
if (_.isFunction(res.redirect)) {
res.redirect(redirectTo);
}
};
}
};
// Ensure our 'this' context in the functions
_.bindAll(errors, "throwError", "logError", "logAndThrowError", "logErrorWithMessage", "logErrorWithRedirect");
module.exports = errors;