首页 > 解决方案 > 获取导致异常的对象信息

问题描述

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

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

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

标签: rubyexception-handlingruby-on-rails-3.2

解决方案


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

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

推荐阅读