2013-05-27 04:15:46 +04:00
|
|
|
(function () {
|
2013-05-26 22:51:58 +04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var _ = require('underscore'),
|
|
|
|
errors;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Basic error handling helpers
|
|
|
|
*/
|
|
|
|
errors = {
|
2013-05-27 04:15:46 +04:00
|
|
|
throwError: function (err) {
|
2013-05-26 22:51:58 +04:00
|
|
|
if (!err) {
|
2013-05-27 23:27:41 +04:00
|
|
|
err = new Error("An error occurred");
|
2013-05-26 22:51:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_.isString(err)) {
|
|
|
|
throw new Error(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
},
|
|
|
|
|
|
|
|
logError: function (err) {
|
2013-06-05 07:47:11 +04:00
|
|
|
err = err || "Unknown";
|
2013-05-26 22:51:58 +04:00
|
|
|
// TODO: Logging framework hookup
|
|
|
|
console.log("Error occurred: ", err.message || err);
|
|
|
|
},
|
|
|
|
|
|
|
|
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;
|
|
|
|
}());
|