问题
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