本文介绍了如何在Rails中捕获整个应用程序的ArgumentError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 铁路3.1

  • Ruby 1.9.1

我在 application_controller 中尝试过,但这似乎不起作用。

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

我可能做错了什么?

  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

我要处理的异常

   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'

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'


推荐答案

如果有人仍在寻找答案,我会请参阅此答案

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

application_controller.rb

下rescue_from ActionController :: RoutingError,带有:-> {render_404}

添加

rescue_from ArgumentError,带有:-> {render_404}

这将完全捕获所有 ArgumentError

这篇关于如何在Rails中捕获整个应用程序的ArgumentError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 15:25