Skip to content

Commit

Permalink
hack: make the error middleware always tell us what caused the 500
Browse files Browse the repository at this point in the history
  • Loading branch information
avermeil committed Jul 7, 2020
1 parent afd7d75 commit fc8096f
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/middleware/error-responder.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
const http = require('http');
const _ = require('lodash');
const http = require("http");
const _ = require("lodash");

// This reponder is assuming that all <500 errors are safe to be responded
// with their .message attribute.
// DO NOT write sensitive data into error messages.
function createErrorResponder(_opts) {
const opts = _.merge({
isErrorSafeToRespond: status => status < 500,
}, _opts);
const opts = _.merge(
{
isErrorSafeToRespond: (status) => status < 500,
},
_opts
);

// 4 params needed for Express to know it's a error handler middleware
// eslint-disable-next-line
Expand All @@ -16,14 +19,15 @@ function createErrorResponder(_opts) {
const status = err.status ? err.status : 500;

const httpMessage = http.STATUS_CODES[status];
if (opts.isErrorSafeToRespond(status)) {
// Always tell us what's wrong
if (true || opts.isErrorSafeToRespond(status)) {
// eslint-disable-next-line
message = err.message;
} else {
message = httpMessage;
}

const isPrettyValidationErr = _.has(err, 'errors');
const isPrettyValidationErr = _.has(err, "errors");
const body = isPrettyValidationErr
? JSON.stringify(err)
: { status, statusText: httpMessage, messages: [message] };
Expand Down

0 comments on commit fc8096f

Please sign in to comment.