Working with exceptions in Pry

By now I think everyone agrees that Pry is the best thing to happen to the Rails console since...well, ever. Built-in to pry are a few really cool features that make it much easier to work with exceptions than it was in plain old IRB. Let's take a look.

If you're like me, you use the Rails console a lot. And by now I think everyone agrees that Pry is the best thing to happen to the Rails console since...well, ever.

Built-in to pry are a few really cool features that make it much easier to work with exceptions than it was in plain old IRB.

View the full backtrace

When exceptions happen in Pry (or IRB for that matter) you're presented with a shortened version of the backtrace. This is usually good enough, but not always.

In pry you can see the full backtrace of the most recent exception by using the wtf -v command. If you leave off the -v flag, you get the abbreviated backtrace.

Pry's WTF command Use the wtf command to access the most recent backtrace

Access exception data

Exceptions often have interesting data attached to them. When an exception happens in IRB you can only see the class name and error message. But with Pry you have access to the actual exception object. That means you can reach in and pull out whatever data you need.

To get the most recently raised exception, use the _ex_ variable. Unlike Ruby's built-in $! variable, you don't have to be inside of a rescue block to use it.

Custom exceptions in pry Use the ex local variable to access the most recent exception

Customize how exceptions are displayed

Suppose you always want to see the full backtrace when you're in pry? You can do that by overriding the default exception handler.

Just open up ~/.pryrc and make it look like this:

# This code was mostly taken from the default exception handler. 
# You can see it here: https://github.com/pry/pry/blob/master/lib/pry.rb

Pry.config.exception_handler = proc do |output, exception, _|
  if UserError === exception && SyntaxError === exception
      output.puts "SyntaxError: #{exception.message.sub(/.*syntax error, */m, '')}"
  else
    output.puts "#{exception.class}: #{exception.message}"
    output.puts "from #{exception.backtrace}"   
  end
end

You could even do crazy stuff like log all of your Pry exceptions to Honeybadger. :)

What to do next:
  1. Try Honeybadger for FREE
    Honeybadger helps you find and fix errors before your users can even report them. Get set up in minutes and check monitoring off your to-do list.
    Start free trial
    Easy 5-minute setup — No credit card required
  2. Get the Honeybadger newsletter
    Each month we share news, best practices, and stories from the DevOps & monitoring community—exclusively for developers like you.
    author photo

    Starr Horne

    Starr Horne is a Rubyist and Chief JavaScripter at Honeybadger.io. When she's not neck-deep in other people's bugs, she enjoys making furniture with traditional hand-tools, reading history and brewing beer in her garage in Seattle.

    More articles by Starr Horne
    Stop wasting time manually checking logs for errors!

    Try the only application health monitoring tool that allows you to track application errors, uptime, and cron jobs in one simple platform.

    • Know when critical errors occur, and which customers are affected.
    • Respond instantly when your systems go down.
    • Improve the health of your systems over time.
    • Fix problems before your customers can report them!

    As developers ourselves, we hated wasting time tracking down errors—so we built the system we always wanted.

    Honeybadger tracks everything you need and nothing you don't, creating one simple solution to keep your application running and error free so you can do what you do best—release new code. Try it free and see for yourself.

    Start free trial
    Simple 5-minute setup — No credit card required

    Learn more

    "We've looked at a lot of error management systems. Honeybadger is head and shoulders above the rest and somehow gets better with every new release."
    — Michael Smith, Cofounder & CTO of YvesBlue

    Honeybadger is trusted by top companies like: