Honeybadger now supports causes / nested exceptions

Ruby 2.1 and later support nested exceptions via the Exception#cause method. Now you can view these for any error reported to Honeybadger. This post gives a brief introduction to exception causes in Ruby, and shows you what they look like in the Honeybadger UI.

What happens when you rescue an exception and re-raise another one? It used to be that the original exception was lost forever. But since Ruby 2.1, you can access these nested exceptions via the cause method.

I've already published a more detailed look at causes, but for now a simple example will work. In the code below we raise three nested exceptions, then use the Exception#cause method to access them.

def caused_exception
  begin
    begin
      raise RuntimeError.new("This is the root cause raised at #{ Time.now }")
    rescue
      raise RuntimeError.new("This is the second cause raised at #{ Time.now }")
    end
  rescue
    raise RuntimeError.new("This is the third cause raised at #{ Time.now }")
  end
end


begin
  caused_exception
rescue => e
  puts e              # This is the third cause raised at 2015-08-04 10:07:11 -0700
  puts e.cause        # This is the second cause raised at 2015-08-04 10:07:11 -0700
  puts e.cause.cause  # This is the root cause raised at 2015-08-04 10:07:11 -0700
end

Viewing causes in Honeybadger

As of today, any error reported to Honeybadger will have its cause information attached. You'll find it right underneath the main backtrace in a section titled "Nested Exceptions". The only thing you need to do is to make sure that you're running the latest version of our gem.

Here's what the exception shown above would look like:

causes

Each cause has its own separate backtrace which you can expand by clicking. You have direct links to your git repo and to open the file in a local editor, just like the "normal" backtrace.

causes_expanded

author photo

Starr Horne

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

“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
Try Error Monitoring Free for 15 Days