2

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'
zabumba
  • 12,172
  • 16
  • 72
  • 129
  • please post where you `raise ArgumentError`. if it your `raise`ing. – Roman Kiselenko Jul 13 '14 at 11:35
  • 1
    The 2 errors in your questions seems to be coming from rack middle-ware. They occur in the stack before arriving in the rails app, therefore your application_controller will never be aware about those exceptions. Check this http://stackoverflow.com/questions/6501451/rescue-from-doesnt-rescue-timeouterror-from-views-or-helpers and maybe this https://github.com/hassox/rack-rescue – Benjamin Bouchet Jul 13 '14 at 12:15
  • Watch this railscast - http://railscasts.com/episodes/53-handling-exceptions-revised – Jon Jul 13 '14 at 13:16

1 Answers1

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

Community
  • 1
  • 1
Maged Makled
  • 1,918
  • 22
  • 25