Ruby 异常处理:反向堆栈跟踪顺序
Posted
技术标签:
【中文标题】Ruby 异常处理:反向堆栈跟踪顺序【英文标题】:Ruby Exception Handling: reverse stack-trace order 【发布时间】:2013-07-31 05:25:57 【问题描述】:当 ruby 应用程序遇到异常时,无论框架如何,堆栈跟踪都会首先显示最重要的信息。通常很长,我发现自己向上滚动命令行以查找它的开始位置,而实际上我只需要阅读前一两行。
我想重新排列输出以反转堆栈跟踪的显示顺序。也许我的措辞不正确,但我无法通过快速搜索找到如何做到这一点。
有没有人有任何处理异常输出的经验,可以为我指明正确的方向?
例如:
$ rackup
/Users/Ascended/Ruby/sinatra/genye/app.rb:19:in `include': wrong argument type String (expected Module) (TypeError)
from /Users/Ascended/Ruby/sinatra/genye/app.rb:19:in `block in <class:Application>'
from /Users/Ascended/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/sinatra-1.4.3/lib/sinatra/base.rb:1385:in `configure'
from /Users/Ascended/Ruby/sinatra/genye/app.rb:17:in `<class:Application>'
from /Users/Ascended/Ruby/sinatra/genye/app.rb:11:in `<top (required)>'
from /Users/Ascended/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/backports-3.3.3/lib/backports/tools.rb:328:in `require'
from /Users/Ascended/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/backports-3.3.3/lib/backports/tools.rb:328:in `require_with_backports'
from /Users/Ascended/Ruby/sinatra/genye/config.ru:7:in `block in <main>'
from /Users/Ascended/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
from /Users/Ascended/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
from /Users/Ascended/Ruby/sinatra/genye/config.ru:in `new'
from /Users/Ascended/Ruby/sinatra/genye/config.ru:in `<main>'
from /Users/Ascended/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:49:in `eval'
from /Users/Ascended/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:49:in `new_from_string'
from /Users/Ascended/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:40:in `parse_file'
from /Users/Ascended/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:277:in `build_app_and_options_from_config'
from /Users/Ascended/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:199:in `app'
from /Users/Ascended/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:314:in `wrapped_app'
from /Users/Ascended/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:250:in `start'
from /Users/Ascended/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:141:in `start'
from /Users/Ascended/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rack-1.5.2/bin/rackup:4:in `<top (required)>'
from /Users/Ascended/.rbenv/versions/2.0.0-p0/bin/rackup:23:in `load'
from /Users/Ascended/.rbenv/versions/2.0.0-p0/bin/rackup:23:in `<main>'
$
我想看看这条线:
/Users/Ascended/Ruby/sinatra/genye/app.rb:19:in `include': wrong argument type String (expected Module) (TypeError)
在输出的末尾而不是开头,有效地颠倒了输出的顺序。
【问题讨论】:
异常可以在很多不同的地方打印出来——你在什么情况下试图完成这个? 谢谢丹,我已经用一个例子更新了我的问题,这些错误是我想要影响的。 【参考方案1】:如果你发现异常,你可以尝试这样的事情:
begin
bad_code
rescue Exception => e
puts e.backtrace.reverse
end
【讨论】:
嗯,所以从这个模式中,我可以更改 Ruby 的 Exception 类以在应用程序级别反转输出,但是如果运行像 rackup 这样的命令行函数,输出不会受到什么影响会在应用程序中以编程方式定义吗?以上是关于Ruby 异常处理:反向堆栈跟踪顺序的主要内容,如果未能解决你的问题,请参考以下文章
如何隐藏 Kafka connect api 未处理异常的堆栈跟踪