如何很好地打印我在救援区中捕获的格式化异常?

时间:2017-04-11 14:16:04

标签: ruby-on-rails exception formatting ruby-on-rails-5 stack-trace

我正在使用Rails 5(Ruby 2.4)。我有一个代码块,可以捕获异常并打印出来,我认为是堆栈跟踪......

      begin
    ...
      rescue Exception => e
        errors.push(e)
        puts "#{e.backtrace}"
        raise e
      end

不幸的是,打印出来的东西是完全不可读的,如下面......

["/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:202:in `block in wait_poll'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:193:in `loop'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:193:in `wait_poll'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:154:in `internal_poll'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:278:in `internal_poll'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:148:in `block in poll'", "/Users/nataliab/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/monitor.rb:214:in `mon_synchronize'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:158:in `synchronize'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:148:in `poll'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:717:in `acquire_connection'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:490:in `checkout'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:364:in `connection'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:883:in `retrieve_connection'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_handling.rb:128:in `retrieve_connection'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_handling.rb:91:in `connection'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/relation/delegation.rb:43:in `connection'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/relation/query_methods.rb:105:in `bound_attributes'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/relation.rb:702:in `exec_queries'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/relation.rb:583:in `load'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/relation.rb:260:in `records'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/relation/finder_methods.rb:527:in `find_take'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/relation/finder_methods.rb:100:in `take'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/relation/finder_methods.rb:78:in `find_by'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/querying.rb:7:in `find_by'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/core.rb:187:in `find_by'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.2/lib/active_record/dynamic_matchers.rb:65:in `find_by_name_and_day_and_user_id_and_distance_and_distance_unit_id'", "/Users/nataliab/Documents/workspace/myproject/app/services/abstract_import_service.rb:71:in `process_event_data'", "/Users/nataliab/Documents/workspace/myproject/app/services/running_in_the_usa_event_finder_service.rb:265:in `process_generic_event_link'", "/Users/nataliab/Documents/workspace/myproject/app/services/running_in_the_usa_event_finder_service.rb:118:in `process_event_link'", "/Users/nataliab/Documents/workspace/myproject/app/services/abstract_event_finder_service.rb:37:in `block (2 levels) in process_data'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:348:in `run_task'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:337:in `block (3 levels) in create_worker'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:320:in `loop'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:320:in `block (2 levels) in create_worker'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:319:in `catch'", "/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:319:in `block in create_worker'"]

有没有什么方法可以让我的异常以一种很好的格式化方式打印出来,就像它在Rails中发生时通常打印出来一样?

2 个答案:

答案 0 :(得分:4)

尝试记录如下的异常消息:

begin
  ...
rescue Exception => e
  logger.error e.message
  e.backtrace.each { |line| logger.error line }
end

答案 1 :(得分:3)

我使用awesome_print gem通过输出控制台和rails控制台模式获取格式化输出。

group :development do
  gem 'awesome_print'
end

然后在Rails控制台中,只需在任何命令,变量,哈希或查询前面添加ap

> ap any_command_or_any_variable_or_query

并通过put输入代码

puts "#{ap e.backtrace}#

使用JSON数据和API调用时非常有用。还可以在格式化输出中轻松检查错误