Set correct error codes for Hanami app exceptions #7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The BetterErrors middleware always returns a 500 status when rescuing an exception (outside of Rails). This is not not always appropriate, such as for a
Hanami::Router::NotFoundError
, which should be a 404.To account for this, gently patch
BetterErrors::Middleware#show_error_page
(which is called only when an exception has been rescued) to pass that rescued exception to a proc we inject into the rack env here in our own middleware. This allows our middleware to know the about exception class and provide the correct status code after BetterErrors is done with its job.This is a monkey-patch, yes, but BetterErrors is very mature gem right now, with most of its code unchanged in 10 years. I'm comfortable with this arrangement until we can either (a) introduce some change to BetterErrors itself to let us directly control the status code, or (b) introduce our own Hanami-owned replacement to BetterErrors. Both of these would be well after Hanami v2.2.