Sometimes I need to debug some nasty exception that has its backtrace hidden or truncated, like an ArgumentError
without any stack trace.
I am used to debugging with byebug. The problem is that the byebug interpreter is a REPL, so it's not possible to write multiline code. I am trying to figure out how to make an inline rescue and print the backtrace from there, ie I want an inline, REPL compatible, version of
begin
....
rescue => e
puts e.backtrace.join("\n")
end
I have tried
begin; my_crashing_method.call; rescue Exception => e; puts e.backtrace; end
But that line raises a SyntaxError
*** SyntaxError Exception: (byebug):1: syntax error, unexpected keyword_rescue
rescue Exception => e
^
I'm not sure what I am missing ?
EDIT
The line above works fine on a regular IRB/Rails shell but not from a byebug shell
IRB
begin my_crashing_method.call; rescue Exception => e; puts e.backtrace end
Stack Trace shows successfully
Byebug
(byebug) begin; my_crashing_method.call; rescue Exception => e; puts e.backtrace
*** SyntaxError Exception: (byebug):1: syntax error, unexpected end-of-input
begin
^
nil
*** NameError Exception: undefined local variable or method `my_crashing_method' for #<StaticPagesController:0x007fae79f61088>
nil
*** SyntaxError Exception: (byebug):1: syntax error, unexpected keyword_rescue
rescue Exception => e
^
nil
*** NameError Exception: undefined local variable or method `e' for #<StaticPagesController:0x007fae79f61088>
nil
When you enter multiple lines ruby code or in a single line on byebug, you need to escape the semicolon using backlash. The following should do the trick.
begin\; my_crashing_method.call\; rescue Exception => e\; puts e.backtrace end
https://github.com/deivid-rodriguez/byebug/blob/master/GUIDE.md#command-syntax
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With