【问题标题】:How do I catch ArgumentError for my whole app in Rails?如何在 Rails 中为我的整个应用程序捕获 ArgumentError?
【发布时间】:2014-09-03 12:31:45
【问题描述】:

环境

  • 铁路 3.1
  • Ruby 1.9.1

我已经尝试过application_controller,但这似乎不起作用。

我可能做错了什么?

  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'

【问题讨论】:

标签: ruby-on-rails ruby exception


【解决方案1】:

如果有人仍在为此寻找答案,我会参考这个答案 https://stackoverflow.com/a/25842118/697816

application_controller.rb

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

添加

rescue_from ArgumentError, with: -> { render_404 }

它将捕获你所有的ArgumentError

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 2011-02-17
    • 2014-04-23
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    相关资源
    最近更新 更多