fix(handled-errors): Handled errors should default to 200

This commit is contained in:
Berkeley Martinez
2017-12-28 11:19:31 -08:00
committed by mrugesh mohapatra
parent 6e7b7a404e
commit d5e7bd586d
2 changed files with 14 additions and 15 deletions

View File

@ -45,22 +45,19 @@ export default function prodErrorHandler() {
// 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;
const handled = unwrapHandledError(err);
// respect handled error status
let status = handled.status || err.status || res.statusCode;
if (!handled.status && status < 400) {
status = 500;
}
res.status(status);
// parse res type
const accept = accepts(req);
const type = accept.type('html', 'json', 'text');
const handled = unwrapHandledError(err);
const redirectTo = handled.redirectTo || '/map';
const redirectTo = handled.redirectTo || '/';
const message = handled.message ||
'Oops! Something went wrong. Please try again later';
@ -77,7 +74,7 @@ export default function prodErrorHandler() {
stack: createStackHtml(err),
errorTitle: createErrorTitle(err),
title: 'freeCodeCamp - Server Error',
status: err.statusCode
status
}
);
}

View File

@ -11,18 +11,20 @@ export function unwrapHandledError(err) {
export function wrapHandledError(err, {
type,
message,
redirectTo
redirectTo,
status = 200
}) {
err[_handledError] = { type, message, redirectTo };
err[_handledError] = { type, message, redirectTo, status };
return err;
}
export const createValidatorErrorFormatter = (type, redirectTo) =>
export const createValidatorErrorFormatter = (type, redirectTo, status) =>
({ msg }) => wrapHandledError(
new Error(msg),
{
type,
message: msg,
redirectTo
redirectTo,
status
}
);