2017-12-28 00:41:42 +00:00
|
|
|
import { inspect } from 'util';
|
|
|
|
import _ from 'lodash/fp';
|
2015-08-04 08:25:34 +00:00
|
|
|
import accepts from 'accepts';
|
2017-07-13 18:39:07 +00:00
|
|
|
import { unwrapHandledError } from '../utils/create-handled-error.js';
|
2015-08-04 08:25:34 +00:00
|
|
|
|
2017-12-28 00:41:42 +00:00
|
|
|
const isDev = process.env.NODE_ENV !== 'production';
|
|
|
|
|
|
|
|
const toString = Object.prototype.toString;
|
|
|
|
// is full error or just trace
|
|
|
|
// _.toString(new Error('foo')) => "Error: foo
|
|
|
|
// Object.prototype.toString.call(new Error('foo')) => "[object Error]"
|
|
|
|
const isInspect = val => !val.stack && _.toString(val) === toString.call(val);
|
|
|
|
const stringifyErr = val => {
|
|
|
|
if (val.stack) {
|
|
|
|
return String(val.stack);
|
2015-08-04 08:25:34 +00:00
|
|
|
}
|
2017-12-28 00:41:42 +00:00
|
|
|
|
|
|
|
const str = String(val);
|
|
|
|
|
|
|
|
return isInspect(val) ?
|
|
|
|
inspect(val) :
|
|
|
|
str;
|
|
|
|
};
|
|
|
|
|
|
|
|
const createStackHtml = _.flow(
|
|
|
|
_.cond([
|
|
|
|
[isInspect, err => [err]],
|
|
|
|
// may be stack or just err.msg
|
|
|
|
[_.stubTrue, _.flow(stringifyErr, _.split('\n'), _.tail) ]
|
|
|
|
]),
|
|
|
|
_.map(_.escape),
|
|
|
|
_.map(line => `<li>${line}</lin>`),
|
|
|
|
_.join('')
|
|
|
|
);
|
|
|
|
|
|
|
|
const createErrorTitle = _.cond([
|
|
|
|
[
|
|
|
|
_.negate(isInspect),
|
|
|
|
_.flow(stringifyErr, _.split('\n'), _.head, _.defaultTo('Error'))
|
|
|
|
],
|
|
|
|
[_.stubTrue, _.constant('Error')]
|
|
|
|
]);
|
|
|
|
|
|
|
|
export default function prodErrorHandler() {
|
2015-08-04 08:25:34 +00:00
|
|
|
// error handling in production.
|
|
|
|
// disabling eslint due to express parity rules for error handlers
|
|
|
|
return function(err, req, res, next) { // eslint-disable-line
|
|
|
|
// respect err.status
|
|
|
|
if (err.status) {
|
|
|
|
res.statusCode = err.status;
|
|
|
|
}
|
|
|
|
|
|
|
|
// default status code to 500
|
|
|
|
if (res.statusCode < 400) {
|
|
|
|
res.statusCode = 500;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse res type
|
2016-07-16 17:40:14 +00:00
|
|
|
const accept = accepts(req);
|
|
|
|
const type = accept.type('html', 'json', 'text');
|
2017-07-13 18:39:07 +00:00
|
|
|
const handled = unwrapHandledError(err);
|
2015-08-04 08:25:34 +00:00
|
|
|
|
2017-07-13 18:39:07 +00:00
|
|
|
const redirectTo = handled.redirectTo || '/map';
|
|
|
|
const message = handled.message ||
|
|
|
|
'Oops! Something went wrong. Please try again later';
|
2017-12-28 00:41:42 +00:00
|
|
|
|
2017-12-28 01:34:56 +00:00
|
|
|
if (isDev) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
|
2015-08-04 08:25:34 +00:00
|
|
|
if (type === 'html') {
|
2017-12-28 00:41:42 +00:00
|
|
|
if (isDev) {
|
|
|
|
return res.render(
|
|
|
|
'dev-error',
|
|
|
|
{
|
|
|
|
...handled,
|
|
|
|
stack: createStackHtml(err),
|
|
|
|
errorTitle: createErrorTitle(err),
|
|
|
|
title: 'freeCodeCamp - Server Error',
|
|
|
|
status: err.statusCode
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2015-08-16 22:26:43 +00:00
|
|
|
if (typeof req.flash === 'function') {
|
2017-07-13 18:39:07 +00:00
|
|
|
req.flash(
|
|
|
|
handled.type || 'errors',
|
|
|
|
{ msg: message }
|
|
|
|
);
|
2015-08-16 22:26:43 +00:00
|
|
|
}
|
2017-07-13 18:39:07 +00:00
|
|
|
return res.redirect(redirectTo);
|
2015-08-04 08:25:34 +00:00
|
|
|
// json
|
|
|
|
} else if (type === 'json') {
|
|
|
|
res.setHeader('Content-Type', 'application/json');
|
|
|
|
return res.send({
|
2017-07-13 18:39:07 +00:00
|
|
|
message
|
2015-08-04 08:25:34 +00:00
|
|
|
});
|
|
|
|
// plain text
|
|
|
|
} else {
|
|
|
|
res.setHeader('Content-Type', 'text/plain');
|
|
|
|
return res.send(message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|