module LunaPark::Extensions::Exceptions::Substitutive::ClassMethods

Public Instance Methods

substitute(origin) click to toggle source

Substitute original exception with save original backtrace.

@example bad case

class MyException < StandardError
  extend LunaPark::Extensions::Exceptions::Substitutive
end

  call_exceptional_lib!
rescue ExceptionalLib::SomeException => e
  raise MyException # => raised MyException with backtrace started from `raise MyException`
                    #      and not contained origin exception backtrace
                    #      that can be very painfull for debug
end

@example resolve

begin
  call_exceptional_lib!
rescue ExceptionalLib::SomeException => e
  raise MyException.substitute(e) # => raised MyException with backtrace started
                                  #      from library `raise ExceptionalLib::SomeException`
                                  #      so you can easily find out where exception starts
end
# File lib/luna_park/extensions/exceptions/substitutive.rb, line 36
def substitute(origin)
  new = new(origin.message)
  new.backtrace = origin.backtrace
  new.origin    = origin
  new
end