Ruby: Prepend info to exception messages without touching stacktrace -


i need prepend debugging info exceptions occur within block, don't want mess backtrace.$! doesn't seem allowing in 1.9.3; raise replacing backtrace regardless of try.

ideas?

here's using originally:

def self.load(filename, virtual_path = nil)   t = template.new(filename, virtual_path)   t.is_page? ? page.new(t) : t rescue   raise $!, "error loading template '#{filename}'#{virtual_path ? " under virtual path '" + virtual_path  + "'" : ""}: #{$!}" end 

the best i've found far this:

def self.load(filename, virtual_path = nil)   t = template.new(filename, virtual_path)   t.is_page? ? page.new(t) : t rescue => e     raise e, "error loading template '#{filename}'#{virtual_path ? " under virtual path '" + virtual_path  + "'" : ""}: #{e.message} #{e.backtrace}" end 

this dumps original stack trace message, still doesn't preserve old stack trace stack trace

if @ kernel#raise method, can take 3 parameters:

raise(exception [, string [, array]]) 

if want keep backtrace, should specify array parameter, callback information.

example:

say had:

def some_method()     raise('original message') end  some_method #=> scratch.rb:10:in `some_method': original message (runtimeerror) #   scratch.rb:16:in `<main>' 

you can use third parameter of exception raise new exception updated message , same backtrace:

def some_method()     begin         raise('error message')     rescue         raise $!, 'new message', $!.backtrace     end end  some_method #=> scratch.rb:10:in `some_method': new message (runtimeerror) #       scratch.rb:16:in `<main>' 

as can see, new exception same original exception, except updated message.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

android - Keyboard hides my half of edit-text and button below it even in scroll view -