Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rescue ActionController::UnknownFormat from raising an exception

I am trying to make it so that an ActionController::UnknownFormat will not raise an exception report in production. I'm using Rails 4 and thought something like this would do the trick, but it doesn't seem to make a difference:

application.rb

config.action_dispatch.rescue_responses.merge!('ActionController::UnknownFormat' => :not_found)
like image 303
Tom Rossi Avatar asked Jan 16 '14 14:01

Tom Rossi


2 Answers

It looks like this was deprecated in Rails 4 in favor of this rescue_from syntax. So something like this:

application_controller.rb:

  rescue_from ActionController::UnknownFormat, with: :raise_not_found

  def raise_not_found
    render(text: 'Not Found', status: 404)
  end
like image 151
Tom Rossi Avatar answered Dec 07 '22 11:12

Tom Rossi


It should not return status code 404, should return status code 415 i.e unsupported_media_type

rescue_from ActionController::UnknownFormat, with: :raise_not_found

def raise_not_found
  render(text: 'Not Found', status: :unsupported_media_type)
end
like image 34
Vishal Zambre Avatar answered Dec 07 '22 10:12

Vishal Zambre