How do I catch ArgumentError for my whole app in Rails?

不羁的心 提交于 2019-12-24 01:47:11

问题


Environment

  • RAILS 3.1
  • Ruby 1.9.1

I have tried in the application_controller but that doesn't seem to work.

Anything I may be doing wrong?

  rescue_from ArgumentError do |exception|
     flash.now[:error] = "Arguments for your request are incorrect"
     #ExceptionNotifier::Notifier.background_exception_notification(exception).deliver if Rails.env.production?
     redirect_to root_url, :alert => exception.message
  end

The exceptions I am trying to deal with

   A ArgumentError occurred in marketplace#index:

  invalid byte sequence in UTF-8
  .bundle/gems/ruby/1.9.1/gems/rack-1.4.5/lib/rack/utils.rb:104:in `normalize_params'

or

A ArgumentError occurred in connect#index:

  invalid %-encoding (%u2713)
  .bundle/gems/ruby/1.9.1/gems/rack-1.4.5/lib/rack/backports/uri/common_192.rb:46:in `decode_www_form_component'

回答1:


If anyone is still looking for an answer for this, I would refer to this answer https://stackoverflow.com/a/25842118/697816

in the application_controller.rb under

rescue_from ActionController::RoutingError, with: -> { render_404 }

add

rescue_from ArgumentError, with: -> { render_404 }

which will catch all your ArgumentError all over



来源:https://stackoverflow.com/questions/24722176/how-do-i-catch-argumenterror-for-my-whole-app-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!