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

Return appropriate response statuses based on error type #1330

Merged
merged 7 commits into from Sep 13, 2023
Merged

Conversation

timriley
Copy link
Member

@timriley timriley commented Sep 11, 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 Set correct error codes for Hanami app exceptions 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.

When both render_errors *and* render_detailed_errors are disabled, return the default responses from hanami-router for not found errors (HTTP 404) and not allowed errors (HTTP 405).

This fixes an bug where not found errors were being rendered as HTTP 500 responses, showing the stack trace for a Hanami::Router::NotFoundError exception.
@timriley timriley self-assigned this Sep 11, 2023
@timriley timriley added this to the v2.1.0 milestone Sep 11, 2023
lib/hanami/slice.rb Outdated Show resolved Hide resolved
@timriley timriley changed the title Return plain HTTP errors when not rendering errors Return plain HTTP responses when not rendering errors Sep 11, 2023
Comment on lines -59 to +67
request = Rack::Request.new(env)
raise unless @config.render_errors

if @config.render_errors
render_exception(request, exception)
else
raise exception
end
render_exception(env, exception)
end

private

def render_exception(request, exception)
def render_exception(env, exception)
request = Rack::Request.new(env)
Copy link
Member Author

Choose a reason for hiding this comment

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

The changes here are just a refactor only. I just noticed the opportunity while I was looking for the fix.

Alas, the currying was quite, but this is simpler and more resilient to changes within hanami-router.
lib/hanami/slice.rb Outdated Show resolved Hide resolved
jodosha

This comment was marked as resolved.

@timriley

This comment was marked as resolved.

@timriley

This comment was marked as resolved.

@timriley timriley changed the title Return plain HTTP responses when not rendering errors Return appropriate response statuses based on error type Sep 12, 2023
This at least reduces the duplication of the `Hanami.bundled?(“hanami-webconsole”)` checks.
@timriley timriley merged commit 9bbc973 into main Sep 13, 2023
6 checks passed
@timriley timriley deleted the fix-404-errors branch September 13, 2023 11:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Not found route returns 500 instead of 404
2 participants