middy: Improve Http Error messages
I altered the httpErrorHandler to follow the JSON spec http://jsonapi.org/examples/#error-objects-basics.
Should this change get applied across the board? Or should I just use this as my own custom middleware?
Whats the default lambda integration you guys are using? (we typically use lambda-proxy as the serverless framework default)
Here is my altered error handler for nice errors =)
const { HttpError } = require('http-errors')
module.exports = () => ({
onError: (handler, next) => {
if (handler.error instanceof HttpError) {
// as per JSON spec http://jsonapi.org/examples/#error-objects-basics
handler.response = {
body: JSON.stringify({
errors: [{
status: handler.error.statusCode,
message: handler.error.message,
detail: handler.error.details
}]
})
}
return next()
}
return next(handler.error)
}
})
It looks like:

Before with body: handler.error.message (no json response)

Love this freakin project! Great work š
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 14
- Comments: 15 (11 by maintainers)
BTW hereās the middleware I ended up writing: https://github.com/dbartholomae/middy-middleware-json-error-handler
In the end is just a matter of specific use cases vs common patterns, guess is better to wait for the community to grow (I am sure it will grow!). Right now I have āmiddified almost everything in my lambdasā.
Following @theburningmonk advices above I have created a set of custom errors for my libraries (data layer, services), that break the execution chain and are handled by a set of error middlewares.
I love the idea of using
httpErrorsto manage āhandledā exceptions, so Iām catching the incoming custom errors and transforming them into common sensehttpErrorsin a specific middleware, I keep the traces and messages in ādevelopment modeā or just standard messages in production to hide the implementation as suggested.I would be nice to have access to other patterns and use cases from other users (perhaps a gitter channel or is too early?). I would watch the repo and see if I can PR something š
I came here after dealing myself with the same error pointed out by @DavidWells when implementing my custom error handling middleware. I ended up almost copying his solution to correctly follow the JSON API spec.
At least for the lambda proxy integration, the error response should change the body format or the content header, otherwise the response is not validā¦
PS: Awesome work guys! I LOVE this project
@DavidWells @jacintoArias Have you tried https://github.com/willfarrell/middy-jsonapi, Iāve been using it for about 2y now without issue.
As for handling different content types, keep in mind that we might want to consider binary formats as well. What if you always inspect the content-type in the response, and defaults to JSON if itās not specified, and bundle only JSON handling in middy by default.
You can then later introduce XML, proto-buf and other formats as middleware? (in the case of protobuf, you also have to set the
isBase64Encodedto true, for instance)