Ruby异常处理:反向堆栈跟踪顺序

时间:2013-07-30 21:21:54

标签: ruby exception stack-trace reverse

当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 个答案:

答案 0 :(得分:0)

如果您正在捕捉异常,可以尝试这样的事情:

begin
  bad_code
rescue Exception => e 
  puts e.backtrace.reverse
end