【问题标题】:How to get ruby class name when an exception is thrown in Ruby在Ruby中引发异常时如何获取ruby类名
【发布时间】:2019-08-04 07:51:41
【问题描述】:

破解一个用 1.8.7 版本编写的古老的 ruby​​ 应用程序 通过 rescue_action_in_public_with_custom 记录所有覆盖 rescue_action_in_public 的未处理异常。我可以看到错误堆栈。但是,如果我可以提取错误发起者类名,那将是一个很大的帮助。例如 -

module Module1
   module Module2
      class Trap
         raise 'exception raised and not handled'
         def do_something
            raise 'something happened in runtime and not handled'
         end
      end
   end
end

我想从 rescue_action_in_public_with_custom 中记录类名“Trap”。任何帮助/想法表示赞赏。

【问题讨论】:

  • 抱歉,rescue_action_in_public 是什么?
  • @Stefan 显然是 rails 2 方法 ActionController::Rescue#rescue_action_in_public
  • 上面的代码在类加载阶段引发。
  • 您可以记录异常的backtrace 并遍历列出的文件位置以找到根本原因。
  • 是的@Stefan,您对记录回溯是正确的,这已经使用 airbrake/errbit 完成。作为一个巨大的流量应用程序,现在我们希望这些日志在ELK 堆栈中找出模式和优先级。

标签: ruby exception ruby-on-rails-2 unhandled-exception rescue


【解决方案1】:

需要考虑的是使用自定义错误类型 -

class ErrorsWithCallerClass < StandardError # or something more appropriate
    attr_reader :klass
    def initialize(msg, klass) 
       @klass = klass 
       super(msg)
    end
end

然后将其与

一起使用
raise ErrorsWithCallerClass.new("bad stuff", Trap) 

【讨论】:

    猜你喜欢
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    • 2012-01-13
    • 2012-06-22
    • 2013-10-19
    • 2013-01-24
    相关资源
    最近更新 更多