缩短终端中的错误消息

时间:2015-05-26 13:21:33

标签: ruby-on-rails ruby error-handling terminal

终端中的Rails错误消息太长,并且通常包含无用的信息。是否有一个宝石/解决方案来缩短rails错误消息? 例如:

2.2.2 :012 >   puts 1.red

我目前得到的内容:

NoMethodError: undefined method `red' for 1:Fixnum
from (irb):12
from /Users/Ben/.rvm/gems/ruby-2.2.2@global/gems/railties-       4.2.1/lib/rails/commands/console.rb:110:in `start'
from /Users/Ben/.rvm/gems/ruby-2.2.2@global/gems/railties-4.2.1/lib/rails/commands/console.rb:9:in `start'
from /Users/Ben/.rvm/gems/ruby-2.2.2@global/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/Ben/.rvm/gems/ruby-2.2.2@global/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/Ben/.rvm/gems/ruby-2.2.2@global/gems/railties-4.2.1/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/Ben/.rvm/gems/ruby-2.2.2@global/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
from /Users/Ben/.rvm/gems/ruby-2.2.2@global/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `block in require'
from /Users/Ben/.rvm/gems/ruby-2.2.2@global/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/Ben/.rvm/gems/ruby-2.2.2@global/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
from /Users/Ben/coding/krokoapp/bin/rails:8:in `<top (required)>'
from /Users/Ben/.rvm/gems/ruby-2.2.2@global/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
from /Users/Ben/.rvm/gems/ruby-2.2.2@global/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/Ben/.rvm/gems/ruby-2.2.2@global/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/Ben/.rvm/gems/ruby-2.2.2@global/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
from /Users/Ben/.rvm/gems/ruby-2.2.2@rails-devise-roles/gems/spring-1.3.6/lib/spring/commands/rails.rb:6:in `call'
from /Users/Ben/.rvm/gems/ruby-2.2.2@rails-devise-roles/gems/spring-1.3.6/lib/spring/command_wrapper.rb:38:in `call'
from /Users/Ben/.rvm/gems/ruby-2.2.2@rails-devise-roles/gems/spring-1.3.6/lib/spring/application.rb:183:in `block in serve'
from /Users/Ben/.rvm/gems/ruby-2.2.2@rails-devise-roles/gems/spring-1.3.6/lib/spring/application.rb:156:in `fork'
from /Users/Ben/.rvm/gems/ruby-2.2.2@rails-devise-roles/gems/spring-1.3.6/lib/spring/application.rb:156:in `serve'
from /Users/Ben/.rvm/gems/ruby-2.2.2@rails-devise-roles/gems/spring-1.3.6/lib/spring/application.rb:131:in `block in run'
from /Users/Ben/.rvm/gems/ruby-2.2.2@rails-devise-roles/gems/spring-1.3.6/lib/spring/application.rb:125:in `loop'
from /Users/Ben/.rvm/gems/ruby-2.2.2@rails-devise-roles/gems/spring-1.3.6/lib/spring/application.rb:125:in `run'
from /Users/Ben/.rvm/gems/ruby-2.2.2@rails-devise-roles/gems/spring-1.3.6/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/Ben/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/Ben/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'2.2.2 :013 >  

我想要的是什么:(可选为红色)

NoMethodError: undefined method `red' for 1:Fixnum
===>Long error message saved in e. Get full messages with 'puts e' 

这可能吗?

1 个答案:

答案 0 :(得分:4)

您可以尝试 better_errors gem。 它还通过浏览器提供了更好的错误解释。

这是link to the github repo

修改

如果您还要查看可自定义的控制台(也是异常跟踪),请尝试查看pry