获取导致异常的对象信息

时间:2018-06-01 07:37:17

标签: ruby exception-handling ruby-on-rails-3.2

我编写了以下代码来处理我的异常。

class Business < ExceptionController
  def work(arg1,arg2)
        #####some business logic that cause exception
  end
end

class ExceptionController < ApplicationController
  rescue_from Exception, :with => :render_error_response

  def render_error_response(e)
     p e.message
     p e.backtrace
  end
end

我在异常控制器中定义的render_error_response中记录消息和回溯。我想打印函数的参数,即导致异常的arg1 arg2函数的work,{{1}}。

除了异常回溯之外,我还需要有关调用def工作的对象信息。

1 个答案:

答案 0 :(得分:1)

您必须自己修饰异常消息。

class Business < ExceptionController
  def work(arg1, arg2)
    #####some business logic that cause exception
  rescue => ex
    ex.message << (" (arg1: %p, arg2: %p, self: %p)" % [arg1, arg2, self])
    raise ex
  end
end