2

这是我对 rails 中的 strack 跟踪的烦恼——它们首先打印错误,这意味着查看任何错误,您已经在您的 termanl 中向上滚动以查看错误是什么。

有没有一种方法可以以相反的顺序打印错误和 stracktrace,因此您无需滚动即可查看错误。例如:

这是 rails/ruby 当前打印的 stracktraces:

      /activesupport/lib/active_support/dependencies.rb:228:in `require': cannot load such file -- pry (LoadError)
        from /activesupport/lib/active_support/dependencies.rb:228:in `block in require'
        from /activesupport/lib/active_support/dependencies.rb:213:in `load_dependency'
        from /activesupport/lib/active_support/dependencies.rb:228:in `require'
        from /actionpack/lib/action_dispatch/routing/mapper.rb:307:in `normalize_path'
        from /actionpack/lib/action_dispatch/routing/mapper.rb:79:in `normalize_path!'
        from /actionpack/lib/action_dispatch/routing/mapper.rb:65:in `initialize'
        from /actionpack/lib/action_dispatch/routing/mapper.rb:1429:in `new'
        from /actionpack/lib/action_dispatch/routing/mapper.rb:1429:in `add_route'
        from /actionpack/lib/action_dispatch/routing/mapper.rb:1408:in `decomposed_match'
        from /actionpack/lib/action_dispatch/routing/mapper.rb:1389:in `block in match'
        from /actionpack/lib/action_dispatch/routing/mapper.rb:1380:in `each'
        from /actionpack/lib/action_dispatch/routing/mapper.rb:1380:in `match'
        from /actionpack/lib/action_dispatch/routing/mapper.rb:502:in `mount'
        from /ruby-2.0.0-p0/bundler/gems/sprockets-rails-bbf755e8b3aa/lib/sprockets/railtie.rb:125:in `block (2 levels) in <class:Railtie>'
        from /actionpack/lib/action_dispatch/routing/route_set.rb:315:in `instance_exec'
        from /actionpack/lib/action_dispatch/routing/route_set.rb:315:in `eval_block'
        from /actionpack/lib/action_dispatch/routing/route_set.rb:330:in `block in clear!'
        from /actionpack/lib/action_dispatch/routing/route_set.rb:330:in `each'
        from /actionpack/lib/action_dispatch/routing/route_set.rb:330:in `clear!'
        from /railties/lib/rails/application/routes_reloader.rb:35:in `block in clear!'
        from /railties/lib/rails/application/routes_reloader.rb:33:in `each'
        from /railties/lib/rails/application/routes_reloader.rb:33:in `clear!'
        from /railties/lib/rails/application/routes_reloader.rb:15:in `reload!'
        from /railties/lib/rails/application/routes_reloader.rb:26:in `block in updater'
        from /activesupport/lib/active_support/file_update_checker.rb:75:in `call'
        from /activesupport/lib/active_support/file_update_checker.rb:75:in `execute'
        from /railties/lib/rails/application/routes_reloader.rb:27:in `updater'
        from /railties/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
        from /railties/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
        from /railties/lib/rails/initializable.rb:30:in `instance_exec'
        from /railties/lib/rails/initializable.rb:30:in `run'
        from /railties/lib/rails/initializable.rb:55:in `block in run_initializers'
        from ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:150:in `block in tsort_each'
        from ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:183:in `block (2 levels) in each_strongly_connected_component'
        from ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:219:in `each_strongly_connected_component_from'
        from ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:182:in `block in each_strongly_connected_component'
        from ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:180:in `each'
        from ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:180:in `each_strongly_connected_component'
        from ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:148:in `tsort_each'
        from /railties/lib/rails/initializable.rb:54:in `run_initializers'
        from /railties/lib/rails/application.rb:214:in `initialize!'
        from /railties/lib/rails/railtie/configurable.rb:30:in `method_missing'
        from /demo/config/environment.rb:5:in `<top (required)>'
        from /demo/config.ru:3:in `require'
        from /demo/config.ru:3:in `block in <main>'
        from /ruby-2.0.0-p0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
        from /ruby-2.0.0-p0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
        from /demo/config.ru:in `new'
        from /demo/config.ru:in `<main>'
        from /ruby-2.0.0-p0/gems/rack-1.5.2/lib/rack/builder.rb:49:in `eval'
        from /ruby-2.0.0-p0/gems/rack-1.5.2/lib/rack/builder.rb:49:in `new_from_string'
        from /ruby-2.0.0-p0/gems/rack-1.5.2/lib/rack/builder.rb:40:in `parse_file'
        from /ruby-2.0.0-p0/gems/rack-1.5.2/lib/rack/server.rb:277:in `build_app_and_options_from_config'
        from /ruby-2.0.0-p0/gems/rack-1.5.2/lib/rack/server.rb:199:in `app'
        from /railties/lib/rails/commands/server.rb:48:in `app'
        from /ruby-2.0.0-p0/gems/rack-1.5.2/lib/rack/server.rb:314:in `wrapped_app'
        from /railties/lib/rails/commands/server.rb:75:in `start'
        from /railties/lib/rails/commands.rb:80:in `block in <top (required)>'
        from /railties/lib/rails/commands.rb:75:in `tap'
        from /railties/lib/rails/commands.rb:75:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

有没有一种方法可以让我以相反的顺序获得相同的 stracktrace,所以每次出现错误时我都不必滚动终端。类似的东西:

    from bin/rails:4:in `<main>'
    from bin/rails:4:in `require'
    from /railties/lib/rails/commands.rb:75:in `<top (required)>'
    from /railties/lib/rails/commands.rb:75:in `tap'
    from /railties/lib/rails/commands.rb:80:in `block in <top (required)>'
    from /railties/lib/rails/commands/server.rb:75:in `start'
    from /ruby-2.0.0-p0/gems/rack-1.5.2/lib/rack/server.rb:314:in `wrapped_app'
    from /railties/lib/rails/commands/server.rb:48:in `app'
    from /ruby-2.0.0-p0/gems/rack-1.5.2/lib/rack/server.rb:199:in `app'
    from /ruby-2.0.0-p0/gems/rack-1.5.2/lib/rack/server.rb:277:in `build_app_and_options_from_config'
    from /ruby-2.0.0-p0/gems/rack-1.5.2/lib/rack/builder.rb:40:in `parse_file'
    from /ruby-2.0.0-p0/gems/rack-1.5.2/lib/rack/builder.rb:49:in `new_from_string'
    from /ruby-2.0.0-p0/gems/rack-1.5.2/lib/rack/builder.rb:49:in `eval'
    from /demo/config.ru:in `<main>'
    from /demo/config.ru:in `new'
    from /ruby-2.0.0-p0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
    from /ruby-2.0.0-p0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
    from /demo/config.ru:3:in `block in <main>'
    from /demo/config.ru:3:in `require'
    from /demo/config/environment.rb:5:in `<top (required)>'
    from /railties/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /railties/lib/rails/application.rb:214:in `initialize!'
    from /railties/lib/rails/initializable.rb:54:in `run_initializers'
    from ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:148:in `tsort_each'
    from ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:180:in `each_strongly_connected_component'
    from ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:180:in `each'
    from ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:182:in `block in each_strongly_connected_component'
    from ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:219:in `each_strongly_connected_component_from'
    from ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:183:in `block (2 levels) in each_strongly_connected_component'
    from ruby-2.0.0-p0/lib/ruby/2.0.0/tsort.rb:150:in `block in tsort_each'
    from /railties/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /railties/lib/rails/initializable.rb:30:in `run'
    from /railties/lib/rails/initializable.rb:30:in `instance_exec'
    from /railties/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
    from /railties/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
    from /railties/lib/rails/application/routes_reloader.rb:27:in `updater'
    from /activesupport/lib/active_support/file_update_checker.rb:75:in `execute'
    from /activesupport/lib/active_support/file_update_checker.rb:75:in `call'
    from /railties/lib/rails/application/routes_reloader.rb:26:in `block in updater'
    from /railties/lib/rails/application/routes_reloader.rb:15:in `reload!'
    from /railties/lib/rails/application/routes_reloader.rb:33:in `clear!'
    from /railties/lib/rails/application/routes_reloader.rb:33:in `each'
    from /railties/lib/rails/application/routes_reloader.rb:35:in `block in clear!'
    from /actionpack/lib/action_dispatch/routing/route_set.rb:330:in `clear!'
    from /actionpack/lib/action_dispatch/routing/route_set.rb:330:in `each'
    from /actionpack/lib/action_dispatch/routing/route_set.rb:330:in `block in clear!'
    from /actionpack/lib/action_dispatch/routing/route_set.rb:315:in `eval_block'
    from /actionpack/lib/action_dispatch/routing/route_set.rb:315:in `instance_exec'
    from /ruby-2.0.0-p0/bundler/gems/sprockets-rails-bbf755e8b3aa/lib/sprockets/railtie.rb:125:in `block (2 levels) in <class:Railtie>'
    from /actionpack/lib/action_dispatch/routing/mapper.rb:502:in `mount'
    from /actionpack/lib/action_dispatch/routing/mapper.rb:1380:in `match'
    from /actionpack/lib/action_dispatch/routing/mapper.rb:1380:in `each'
    from /actionpack/lib/action_dispatch/routing/mapper.rb:1389:in `block in match'
    from /actionpack/lib/action_dispatch/routing/mapper.rb:1408:in `decomposed_match'
    from /actionpack/lib/action_dispatch/routing/mapper.rb:1429:in `add_route'
    from /actionpack/lib/action_dispatch/routing/mapper.rb:1429:in `new'
    from /actionpack/lib/action_dispatch/routing/mapper.rb:65:in `initialize'
    from /actionpack/lib/action_dispatch/routing/mapper.rb:79:in `normalize_path!'
    from /actionpack/lib/action_dispatch/routing/mapper.rb:307:in `normalize_path'
    from /activesupport/lib/active_support/dependencies.rb:228:in `require'
    from /activesupport/lib/active_support/dependencies.rb:213:in `load_dependency'
    from /activesupport/lib/active_support/dependencies.rb:228:in `block in require'
  /activesupport/lib/active_support/dependencies.rb:228:in `require': cannot load such file -- pry (LoadError)

可能的?

4

1 回答 1

2

有几种方法可以做到这一点。

如果您在 irb 或调试器中,例如:

caller.reverse

会给你你想要的。然后另一种方法是像这样包装所有代码

begin
  all the code
rescue StandardError => e 
  puts e.backtrace.reverse
end

最后,您可以在 Exception 上扩展回溯以打印 caller.reverse,但扩展核心模块根本不是一个好主意。

于 2013-08-12T14:06:51.223 回答