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
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
commented
Sep 11, 2023
timriley
changed the title
Return plain HTTP errors when not rendering errors
Return plain HTTP responses when not rendering errors
Sep 11, 2023
timriley
commented
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) |
There was a problem hiding this comment.
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.
timriley
force-pushed
the
fix-404-errors
branch
from
September 11, 2023 13:57
7885d75
to
f9489dd
Compare
jodosha
reviewed
Sep 11, 2023
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
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
force-pushed
the
fix-404-errors
branch
from
September 12, 2023 13:08
0869178
to
abb59de
Compare
jodosha
approved these changes
Sep 13, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Properly return 404 errors when the app is called with not found routes.
To do this, cover two scenarios:
app.config.render_detailed_errors
is false, then do not provide our customnot_allowed
andnot_found
procs to the slice's router instance. This means that theHanami::Router
default responses will be returned for these cases, which include the appropriate error codes.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 byBetterErrors::Middleware
and then re-apply an appropriate response code based on theapp.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-moderender_errors
middleware) and provides greater consistency across both dev and production mode.Resolves #1327.