53

I just got this error message:

...
from c:/ruby/lib/ruby/gems/1.8/gems/...
 ... 10 levels...
from c:/ruby/lib/ruby/gems/1.8/gems/...
...

and the bug (of course) is hidden somewhere in ... 10 levels....

How can I force Ruby to show a full stack trace?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Željko Filipin
  • 56,372
  • 28
  • 94
  • 125

1 Answers1

94
begin
  # Code that raises exception
rescue StandardError => e
  puts e.backtrace
end
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Željko Filipin
  • 56,372
  • 28
  • 94
  • 125
  • 8
    How do you show the stacktrace without catching an exception? Just in normal operation, that is. – Chirag Patel Jun 30 '11 at 14:56
  • 9
    @ChiragPatel, maybe you want to use [Kernel#caller](http://www.ruby-doc.org/core-1.8.7/Kernel.html#method-i-caller). – Sony Santos Jan 02 '12 at 16:06
  • 3
    Kernel#caller, as Sony Santos mentioned, is absolutely the way to go for what you are wanting. It will return an array of strings. You could easily just do something like the following to get a neat stack trace in your log: Rails.logger.info caller.join("\n") – slant Dec 31 '12 at 20:13
  • 3
    By default rescue handles **StandardError** not **Exception**. So you should rather rescue StandardError. See https://robots.thoughtbot.com/rescue-standarderror-not-exception. – DanT Mar 18 '15 at 13:29