gpt4 book ai didi

ruby-on-rails - 我怎样才能很好地打印在救援 block 中捕获的格式化异常?

转载 作者:行者123 更新时间:2023-12-04 17:16:16 24 4
gpt4 key购买 nike

我正在使用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内发生异常时通常将其打印出来一样?

最佳答案

尝试像这样记录异常消息:

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

关于ruby-on-rails - 我怎样才能很好地打印在救援 block 中捕获的格式化异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43348556/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com