Skip to content

Commit

Permalink
bug: body-parser failures would throw bare exceptions/500s. now 400.
Browse files Browse the repository at this point in the history
* upgrade body-parser to actually be able to detect its failure signature.
  expressjs/body-parser#122
* also change sendError signature to match the standard one more.
  • Loading branch information
issa-tseng committed Nov 9, 2017
1 parent 710fcdc commit 2581ce8
Show file tree
Hide file tree
Showing 4 changed files with 630 additions and 60 deletions.
2 changes: 1 addition & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ service.use((error, request, response, next) => {
// In this case, we'll just let Express fail the request out.
return next(error);
}
sendError(response, error);
sendError(error, request, response);
});


Expand Down
6 changes: 5 additions & 1 deletion lib/util/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,17 @@ const endpoint = (f) => (request, response, next) => {
// Given a error thrown upstream that is of our own internal format, this
// handler does the necessary work to translate that error into an HTTP error
// and send it out.
const sendError = (response, error) => {
const sendError = (error, request, response) => {
if (error.isProblem === true) {
// we already have a publicly-consumable error object.
response.status(error.httpCode).type('application/json').send({
message: error.message,
code: error.problemCode,
details: error.problemDetails
});
} else if (error.type === 'entity.parse.failed') {
// catch body-parser middleware problems.
sendError(Problem.user.unparseable({ format: 'json', rawLength: error.body.length }), request, response);
} else {
const details = {};
if (error.stack != null)
Expand Down
Loading

0 comments on commit 2581ce8

Please sign in to comment.