Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set correct error codes for Hanami app exceptions #7

Merged
merged 1 commit into from Sep 13, 2023

Conversation

timriley
Copy link
Member

@timriley timriley commented Sep 12, 2023

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.

jodosha

This comment was marked as outdated.

@timriley

This comment was marked as outdated.

Copy link
Member

@jodosha jodosha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timriley Thank you for this fix and the patience to help troubleshoot this problem on my computer.

@timriley timriley self-assigned this Sep 13, 2023
@timriley timriley merged commit 94a0b6a into main Sep 13, 2023
6 checks passed
@timriley timriley deleted the correct-status-codes branch September 13, 2023 11:05
timriley added a commit to hanami/hanami that referenced this pull request Sep 13, 2023
Properly return 404 errors when the app is called with not found routes.

To do this, cover two scenarios:

- When hanami-webconsole **is not bundled**, or when `app.config.render_detailed_errors` is false, then do not provide our custom `not_allowed` and `not_found` procs to the slice's router instance. This means that the `Hanami::Router` default responses will be returned for these cases, which include the appropriate error codes.
- When hanami-webconsole **is bundled**, and when `app.config.render_detailed_errors` is true (which will be the default for all new Hanami 2.1 apps), then rely on the changes in hanami/webconsole#7 to capture the error rescued by `BetterErrors::Middleware` and then re-apply an appropriate response code based on the `app.config.render_error_responses` setting that we already use to map exceptions to response types. This is a nice reuse of this existing setting (which was previously only used for the production-mode `render_errors` middleware) and provides greater consistency across both dev and production mode.

Resolves #1327.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

2 participants