Cyr*_*ris 8 ruby ruby-on-rails byebug
有时我需要调试一些令人讨厌的异常,它的回溯隐藏或截断,就像ArgumentError没有任何堆栈跟踪一样.
我习惯用byebug调试.问题是byebug解释器是一个REPL,因此不可能编写多行代码.我试图找出如何进行内联救援并从那里打印回溯,即我想要一个内联,REPL兼容的版本
begin
....
rescue => e
puts e.backtrace.join("\n")
end
Run Code Online (Sandbox Code Playgroud)
我试过了
begin; my_crashing_method.call; rescue Exception => e; puts e.backtrace; end
Run Code Online (Sandbox Code Playgroud)
但该行引发了一个SyntaxError
*** SyntaxError Exception: (byebug):1: syntax error, unexpected keyword_rescue
rescue Exception => e
^
Run Code Online (Sandbox Code Playgroud)
我不确定我错过了什么?
编辑
上面的行在常规IRB/Rails shell上工作正常,但不能从byebug shell中工作
IRB
begin my_crashing_method.call; rescue Exception => e; puts e.backtrace end
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪显示成功
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
Run Code Online (Sandbox Code Playgroud)
小智 7
当您输入多行红宝石代码或在byebug上的单行中输入时,您需要使用反冲来转义分号。以下应该可以解决问题。
begin\; my_crashing_method.call\; rescue Exception => e\; puts e.backtrace end
Run Code Online (Sandbox Code Playgroud)
https://github.com/deivid-rodriguez/byebug/blob/master/GUIDE.md#command-syntax