0

下面是一些 sudo 代码:

异常.rb

module Exceptions
  class CriticalError < StandardError
    # Question: How do I attach a callback to this error? Whenever this error is raised, I also want it to ping_me() as a after callback
    def ping_me()
      ...
    end
  end
end

期望的结果:

raise Exceptions::CriticalError # after a callback to this error being raised, it will run the ping_me() method

问题:

我如何在rails中做到这一点?我看到了一种rescue_from方法,但我认为它只能在控制器内部使用

非常感谢!

4

1 回答 1

0

好的,我想通了

module Exceptions
  class CriticalError < StandardError
    def initialize(error_message)
      ping_me()
      super(error_message)
    end

    def ping_me()
      ....
    end
  end
end


raise Exceptions::CriticalError.new("something went wrong!")
于 2013-11-06T20:37:15.903 回答