Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable stack dump on exception in Rails console

I am using Ruby on Rails with the pry gem. When an error occurs during a rake task, I get a very nice stack trace.

When however I do something in my rails console that triggers an exception, I only get to see the error message and the one line of code that triggered it (which most of the time is somewhere in the rails core).

Is there a way to enable these stack dumps in the console?

like image 587
Pelle Avatar asked Jul 15 '13 13:07

Pelle


2 Answers

I found a solution myself.

Apparently, I was in need of the command wtf? that comes with pry.

[7] project »  p.known_attributes
NoMethodError: undefined method `foo' for #<Bar:0x007f871fd12a38>
from /[...]/gems/activemodel-4.0.0/lib/active_model/attribute_methods.rb:436:in `method_missing'

[8] project »  wtf?
Exception: NoMethodError: undefined method `foo' for #<Bar:0x007f871fd12a38>
--
[... stack dump ...]  

[9] project »  
like image 199
Pelle Avatar answered Nov 12 '22 16:11

Pelle


When there's an error in console you should see something like this

$ rails c
Loading development environment (Rails 4.0.0)
irb(main):001:0> no_method
NameError: undefined local variable or method `no_method' for main:Object
    from (irb):1
    from /Users/michal/Projects/tennisliga/.gems/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
    from /Users/michal/Projects/tennisliga/.gems/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
    from /Users/michal/Projects/tennisliga/.gems/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

What's the problem with it? You don't see the stack trace?

EDIT: If you're using pry and would like to see more stack trace, refer to pry wiki Short example (show all lines)

Pry.config.exception_handler = proc do |output, exception, _pry_|
  output.puts "#{exception}"
  output.puts "#{exception.backtrace}"
end
like image 2
Mike Szyndel Avatar answered Nov 12 '22 14:11

Mike Szyndel