【问题标题】:Get object information that caused the exception获取导致异常的对象信息
【发布时间】:2018-11-11 07:08:20
【问题描述】:

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

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 中记录消息和回溯。我想打印导致异常的函数的参数,即 arg1arg2work 函数。

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

【问题讨论】:

  • 你最好先让你的代码在语法上有效。

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


【解决方案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

【讨论】:

    猜你喜欢
    • 2016-04-21
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多