Reputation: 189
I'm joining the many people who want to rescue ActionController::UnknownFormat
in their Rails applications but are unable to.
I'd prefer to rescue exceptions I don't care about, both to provide better error pages to misguided uses, and also to keep my exception tracker quiet.
The two cases I'm trying to solve are:
/catalog.xml
(I only respond to HTML requests)The research I've done turned up this solution as the best approach but, unfortunately, it doesn't work for me.
In app/controllers/application_controller.rb
, I have this:
rescue_from ActiveRecord::RecordNotFound, :with => :render_standard_error
rescue_from ActionController::UnknownFormat, :with => :render_standard_error
rescue_from ActionController::InvalidAuthenticityToken, :with => :render_token_error
This works great, except for that ActionController::UnknownFormat
.
The render_standard_error
method just sets up some env defaults and renders an error page.
I expected that rescue_from ActionController::UnknownFormat
would work the way it works for the other error types but the exception is not being caught.
I'm sure I'm missing something terribly obvious here but damned if I can figure it out.
I'm using Rails 5.2.2 with Passenger 5.3.1.
I'm actually seeing two different behaviors:
/catalog.xml
, I get:ActionController::UnknownFormat at /catalog.xml
ProductsController#index is missing a template for this request format and variant.
request.formats: ["application/xml"]
request.variant: []
/catalog.invalid
, the catalog page loads normally, as if I had used just /catalog
.Upvotes: 1
Views: 407
Reputation: 35595
I could not replicate your problem. Here's what I tried:
Stack Overflow could not get the formatting right so I pasted it here: https://gist.github.com/BKSpurgeon/391b0da243b32936334d7989f3e3cadb
Seems to raise and catch the exception just fine?! Please post more information: where are you expecting to raise the error.
Steps
Upvotes: 1