2013-09-19 06:50:06 +04:00
|
|
|
/*jslint regexp: true */
|
2013-12-09 04:49:02 +04:00
|
|
|
var _ = require('underscore'),
|
|
|
|
colors = require('colors'),
|
|
|
|
fs = require('fs'),
|
|
|
|
configPaths = require('./config/paths'),
|
|
|
|
path = require('path'),
|
2014-01-03 19:50:03 +04:00
|
|
|
when = require('when'),
|
2013-09-17 14:56:05 +04:00
|
|
|
errors,
|
|
|
|
|
|
|
|
// Paths for views
|
2013-12-09 04:49:02 +04:00
|
|
|
defaultErrorTemplatePath = path.resolve(configPaths().adminViews, 'user-error.hbs'),
|
|
|
|
userErrorTemplatePath = path.resolve(configPaths().themePath, 'error.hbs'),
|
2014-01-03 00:57:37 +04:00
|
|
|
userErrorTemplateExists = false,
|
2013-12-31 03:13:25 +04:00
|
|
|
|
|
|
|
ONE_HOUR_S = 60 * 60;
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Basic error handling helpers
|
|
|
|
*/
|
|
|
|
errors = {
|
2014-01-03 00:57:37 +04:00
|
|
|
updateActiveTheme: function (activeTheme, hasErrorTemplate) {
|
2013-12-09 04:49:02 +04:00
|
|
|
userErrorTemplatePath = path.resolve(configPaths().themePath, activeTheme, 'error.hbs');
|
2014-01-03 00:57:37 +04:00
|
|
|
userErrorTemplateExists = hasErrorTemplate;
|
2013-10-21 20:59:37 +04:00
|
|
|
},
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
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
|
|
|
|
2014-01-03 19:50:03 +04:00
|
|
|
// ## Reject Error
|
|
|
|
// Used to pass through promise errors when we want to handle them at a later time
|
|
|
|
rejectError: function (err) {
|
|
|
|
return when.reject(err);
|
|
|
|
},
|
|
|
|
|
2013-10-18 01:49:14 +04:00
|
|
|
logWarn: function (warn, context, help) {
|
|
|
|
if ((process.env.NODE_ENV === 'development' ||
|
|
|
|
process.env.NODE_ENV === 'staging' ||
|
|
|
|
process.env.NODE_ENV === 'production')) {
|
|
|
|
|
|
|
|
console.log('\nWarning:'.yellow, warn.yellow);
|
|
|
|
|
|
|
|
if (context) {
|
|
|
|
console.log(context.white);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (help) {
|
|
|
|
console.log(help.green);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a new line
|
|
|
|
console.log('');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-09-15 15:11:47 +04:00
|
|
|
logError: function (err, context, help) {
|
2013-09-19 06:50:06 +04:00
|
|
|
var stack = err ? err.stack : null;
|
2014-01-04 00:07:08 +04:00
|
|
|
if (err) {
|
|
|
|
err = err.message || err || 'An unknown error occurred.';
|
|
|
|
} else {
|
|
|
|
err = 'An unknown error occurred.';
|
|
|
|
}
|
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-09-19 06:50:06 +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
|
|
|
|
2013-09-24 14:46:30 +04:00
|
|
|
console.error('\nERROR:'.red, err.red);
|
2013-09-15 15:11:47 +04:00
|
|
|
|
|
|
|
if (context) {
|
2013-10-18 01:49:14 +04:00
|
|
|
console.error(context.white);
|
2013-09-15 15:11:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (help) {
|
2013-09-19 06:50:06 +04:00
|
|
|
console.error(help.green);
|
2013-09-15 15:11:47 +04:00
|
|
|
}
|
2013-09-19 06:50:06 +04:00
|
|
|
|
2013-09-15 15:11:47 +04:00
|
|
|
// add a new line
|
2013-09-24 14:46:30 +04:00
|
|
|
console.error('');
|
2013-09-19 06:50:06 +04:00
|
|
|
|
|
|
|
if (stack) {
|
2013-09-24 14:46:30 +04:00
|
|
|
console.error(stack, '\n');
|
2013-09-19 06:50:06 +04:00
|
|
|
}
|
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-10-31 22:02:34 +04:00
|
|
|
/*jslint unparam:true*/
|
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) {
|
2013-10-31 22:02:34 +04:00
|
|
|
/*jslint unparam:true*/
|
2013-09-19 06:50:06 +04:00
|
|
|
|
|
|
|
function parseStack(stack) {
|
2013-10-25 23:31:53 +04:00
|
|
|
if (!_.isString(stack)) {
|
2013-09-19 06:50:06 +04:00
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: split out line numbers
|
|
|
|
var stackRegex = /\s*at\s*(\w+)?\s*\(([^\)]+)\)\s*/i;
|
|
|
|
|
|
|
|
return (
|
|
|
|
stack
|
|
|
|
.split(/[\r\n]+/)
|
|
|
|
.slice(1)
|
|
|
|
.map(function (line) {
|
|
|
|
var parts = line.match(stackRegex);
|
|
|
|
if (!parts) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2013-09-24 14:46:30 +04:00
|
|
|
'function': parts[1],
|
|
|
|
'at': parts[2]
|
2013-09-19 06:50:06 +04:00
|
|
|
};
|
|
|
|
})
|
|
|
|
.filter(function (line) {
|
|
|
|
return !!line;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-09-17 14:56:05 +04:00
|
|
|
// Render the error!
|
2013-09-18 09:12:53 +04:00
|
|
|
function renderErrorInt(errorView) {
|
2013-09-19 06:50:06 +04:00
|
|
|
var stack = null;
|
|
|
|
|
2013-09-24 14:46:30 +04:00
|
|
|
if (process.env.NODE_ENV !== 'production' && err.stack) {
|
2013-09-19 06:50:06 +04:00
|
|
|
stack = parseStack(err.stack);
|
|
|
|
}
|
|
|
|
|
2013-09-17 14:56:05 +04:00
|
|
|
// TODO: Attach node-polyglot
|
2013-10-17 23:42:15 +04:00
|
|
|
res.status(code).render((errorView || 'error'), {
|
2013-09-17 14:56:05 +04:00
|
|
|
message: err.message || err,
|
2013-09-19 06:50:06 +04:00
|
|
|
code: code,
|
|
|
|
stack: stack
|
2013-09-17 14:56:05 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (code >= 500) {
|
2013-09-19 06:50:06 +04:00
|
|
|
this.logError(err, "ErrorPage", "Ghost caught a processing error in the middleware layer.");
|
2013-09-17 14:56:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Are we admin? If so, don't worry about the user template
|
2013-10-18 01:49:14 +04:00
|
|
|
if ((res.isAdmin && req.session.user) || 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.
|
2014-01-03 00:57:37 +04:00
|
|
|
return renderErrorInt(defaultErrorTemplatePath);
|
2013-09-17 14:56:05 +04:00
|
|
|
},
|
|
|
|
|
2013-10-18 01:49:14 +04:00
|
|
|
error404: function (req, res, next) {
|
|
|
|
var message = res.isAdmin && req.session.user ? "No Ghost Found" : "Page Not Found";
|
|
|
|
|
2013-12-31 03:13:25 +04:00
|
|
|
// 404 errors should be briefly cached
|
|
|
|
res.set({'Cache-Control': 'public, max-age=' + ONE_HOUR_S});
|
2013-10-18 01:49:14 +04:00
|
|
|
if (req.method === 'GET') {
|
|
|
|
this.renderErrorPage(404, message, req, res, next);
|
|
|
|
} else {
|
|
|
|
res.send(404, message);
|
|
|
|
}
|
2013-09-19 06:50:06 +04:00
|
|
|
},
|
|
|
|
|
2013-10-18 01:49:14 +04:00
|
|
|
error500: function (err, req, res, next) {
|
2013-12-31 03:13:25 +04:00
|
|
|
// 500 errors should never be cached
|
|
|
|
res.set({'Cache-Control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'});
|
|
|
|
|
|
|
|
if (err.status === 404) {
|
|
|
|
return this.error404(req, res, next);
|
|
|
|
}
|
|
|
|
|
2013-10-18 01:49:14 +04:00
|
|
|
if (req.method === 'GET') {
|
|
|
|
if (!err || !(err instanceof Error)) {
|
|
|
|
next();
|
|
|
|
}
|
2013-11-15 18:27:06 +04:00
|
|
|
errors.renderErrorPage(err.status || 500, err, req, res, next);
|
2013-10-18 01:49:14 +04:00
|
|
|
} else {
|
2013-10-21 09:53:26 +04:00
|
|
|
res.send(err.status || 500, err);
|
2013-09-19 06:50:06 +04:00
|
|
|
}
|
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,
|
2013-09-24 14:46:30 +04:00
|
|
|
'throwError',
|
|
|
|
'logError',
|
|
|
|
'logAndThrowError',
|
|
|
|
'logErrorWithRedirect',
|
|
|
|
'renderErrorPage',
|
2013-10-18 01:49:14 +04:00
|
|
|
'error404',
|
|
|
|
'error500'
|
2013-09-17 14:56:05 +04:00
|
|
|
);
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-10-17 23:42:15 +04:00
|
|
|
module.exports = errors;
|