rescue_from被执行两次

时间:2016-02-19 16:46:20

标签: ruby-on-rails ruby google-chrome exception-handling

我正在尝试使用rescue_from从我的控制器中解救RuntimeErrors,将其记录为HealthNotice,然后使用flash消息重定向用户,但我发现在某些情况下,rescue_from方法正在运行两次(我最终得到2个重复的HealthNotices)。如果从方法中调用的函数(例如下面的测试方法)中查询异常,它只创建一个,但如果我直接在方法中引发异常(例如test2),则救援函数运行两次(尽管奇怪的是,我没有得到双重渲染错误 - ??)

class Portal::BluerimController < PortalController
    rescue_from RuntimeError, with: :error_handler
    def error_handler(e)
      hn_long_message = "#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}\n\nPARAMS:\n#{params}"
      HealthNotice.create(name: "API Exception Handled", severity: "WARNING", short_message: e.message, long_message: hn_long_message)
      flash[:notice] = e.message
      redirect_to :action => :index and return
    end

    def test
      Sotal.new.test  # a method that raises a RuntimeError -- only 1 HealthNotice
    end

    def test2
      raise "an error"  # results in duplicate HealthNotice
    end
end

对我做错了什么的任何建议?谢谢!

0 个答案:

没有答案