Fix ruby ​​exclusion elsewhere

How to make an exception reraised from bar in the last line, it looks like it came from there, and not from a block? I want to see bar in the opposite direction.

 begin raise "foo" rescue => e # yeah, i know $e = e # oh boy, globals end sleep 1 # again, i know def bar raise $e end bar # => test.rb:2:in `<main>': foo (RuntimeError) 

Edit:

Current backtrace

 test.rb:2:in `<main>': foo (RuntimeError) 

what i want (or similar)

 test.rb:10:in `bar': foo (RuntimeError) from test.rb:13:in `<main>' 
+4
source share
2 answers

I'm not sure what you need, but you can try:

 begin raise "foo" rescue => e $e = e end sleep 1 def get_full_stack caller end def bar exception = $e.dup exception.set_backtrace get_full_stack raise exception end 
+1
source

I am not sure if this is the correct answer. But I decided to give it back :-)

 begin raise "foo" rescue => e $e = e end sleep 1 def bar raise $e.class, "bar" end bar #=> test.rb:10:in `bar': bar (RuntimeError) from test.rb:13:in `<main>' 

Second attempt

 begin ... end sleep 1 def bar $e.set_backtrace(["bar"]) raise $e end bar #=> bar: foo (RuntimeError) 
+1
source

Source: https://habr.com/ru/post/1502468/


All Articles