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. // error handling in production.
// disabling eslint due to express parity rules for error handlers // disabling eslint due to express parity rules for error handlers
return function(err, req, res, next) { // eslint-disable-line return function(err, req, res, next) { // eslint-disable-line
// respect err.status const handled = unwrapHandledError(err);
if (err.status) { // respect handled error status
res.statusCode = err.status; let status = handled.status || err.status || res.statusCode;
} if (!handled.status && status < 400) {
status = 500;
// default status code to 500
if (res.statusCode < 400) {
res.statusCode = 500;
} }
res.status(status);
// parse res type // parse res type
const accept = accepts(req); const accept = accepts(req);
const type = accept.type('html', 'json', 'text'); const type = accept.type('html', 'json', 'text');
const handled = unwrapHandledError(err);
const redirectTo = handled.redirectTo || '/map'; const redirectTo = handled.redirectTo || '/';
const message = handled.message || const message = handled.message ||
'Oops! Something went wrong. Please try again later'; 'Oops! Something went wrong. Please try again later';
@ -77,7 +74,7 @@ export default function prodErrorHandler() {
stack: createStackHtml(err), stack: createStackHtml(err),
errorTitle: createErrorTitle(err), errorTitle: createErrorTitle(err),
title: 'freeCodeCamp - Server Error', title: 'freeCodeCamp - Server Error',
status: err.statusCode status
} }
); );
} }

View File

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