How to get ruby class name when an exception is thrown in Ruby

1.1k Views Asked by At

Cracking down an age-old ruby application written in version 1.8.7 to log all the unhandled exceptions overriding rescue_action_in_public by rescue_action_in_public_with_custom. I can see the error stack. However, if I can extract the error originator class name, it would be a great help. For example -

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

I want to log the class name "Trap" from within rescue_action_in_public_with_custom. Any help/ideas are appreciated.

1

There are 1 best solutions below

0
Slabgorb On

Something to think about is using a customized error type -

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

then use that with

raise ErrorsWithCallerClass.new("bad stuff", Trap)