Skip to content

fix: handle ActionController::UnknownFormat with 406 response#2600

Draft
mroderick wants to merge 1 commit intocodebar:masterfrom
mroderick:fix/unknown-format-rescue
Draft

fix: handle ActionController::UnknownFormat with 406 response#2600
mroderick wants to merge 1 commit intocodebar:masterfrom
mroderick:fix/unknown-format-rescue

Conversation

@mroderick
Copy link
Copy Markdown
Collaborator

@mroderick mroderick commented Apr 28, 2026

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/json from crawlers/API clients) cause ActionController::UnknownFormat exceptions when controllers don't have templates for those formats.

Example error from production:

ActionController::UnknownFormat: ChapterController#show is missing a template for this request format and variant.
request.formats: [\"application/json\"]

Solution

Uses Rails' built-in rescue_from mechanism to handle ActionController::UnknownFormat exceptions centrally in ApplicationController.

This approach was inspired by @olleolleolle's suggestion to use respond_to blocks in controllers:

respond_to do |format|
  format.html
  format.any { redirect_to support_path }
end

We adapted this to use rescue_from for a more centralized solution that follows the existing pattern in the codebase.

This is the idiomatic Rails approach, as documented in:

"Rails 4.0 raises ActionController::UnknownFormat when an action does not handle the requested format. By default, this exception is handled by responding with 406 Not Acceptable, but developers can now override this behavior."
Rails Upgrade Guide

Changes

  • Add rescue_from ActionController::UnknownFormat, with: :render_not_acceptable 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

Response Behavior

Format Response
HTML Renders errors/not_acceptable template with 406 status
JSON/XML/etc Returns empty response with 406 status

Testing

bundle exec rspec spec/controllers/application_controller_spec.rb

All 66 controller tests pass.

Related

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.
@mroderick mroderick requested a review from olleolleolle April 28, 2026 17:52
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.

1 participant