fix: handle ActionController::UnknownFormat with 406 response#2600
Draft
mroderick wants to merge 1 commit intocodebar:masterfrom
Draft
fix: handle ActionController::UnknownFormat with 406 response#2600mroderick wants to merge 1 commit intocodebar:masterfrom
mroderick wants to merge 1 commit intocodebar:masterfrom
Conversation
Adds rescue_from handler for ActionController::UnknownFormat in ApplicationController, returning 406 Not Acceptable for unsupported request formats. This is the idiomatic Rails approach for handling format negotiation errors, as documented in the Rails Guides and API: - https://api.rubyonrails.org/classes/ActionController/MimeResponds.html - https://github.com/rails/rails/blob/main/guides/source/action_controller_advanced_topics.md Changes: - Add rescue_from ActionController::UnknownFormat to ApplicationController - Add render_not_acceptable method returning 406 status - Add not_acceptable.html.haml error template - Add comprehensive controller tests for HTML, JSON, and XML formats Fixes UnknownFormat errors when crawlers/scripts send non-HTML Accept headers.
This file contains hidden or 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
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.
Summary
Adds idiomatic Rails exception handling for
ActionController::UnknownFormat, returning 406 Not Acceptable for unsupported request formats.Problem
Requests with non-HTML Accept headers (e.g.,
Accept: application/jsonfrom crawlers/API clients) causeActionController::UnknownFormatexceptions when controllers don't have templates for those formats.Example error from production:
Solution
Uses Rails' built-in
rescue_frommechanism to handleActionController::UnknownFormatexceptions centrally inApplicationController.This approach was inspired by @olleolleolle's suggestion to use
respond_toblocks in controllers:We adapted this to use
rescue_fromfor a more centralized solution that follows the existing pattern in the codebase.This is the idiomatic Rails approach, as documented in:
Changes
rescue_from ActionController::UnknownFormat, with: :render_not_acceptabletoApplicationControllerrender_not_acceptablemethod returning 406 statusnot_acceptable.html.hamlerror templateResponse Behavior
errors/not_acceptabletemplate with 406 statusTesting
bundle exec rspec spec/controllers/application_controller_spec.rbAll 66 controller tests pass.
Related
respond_tosuggestion that led to this solution!