Global Exception Handling With Bugsnag

July 9, 2012

At TalentSoup, we recently launched a new application privately to a number of our trusted customers. For exception handling, I decided to give bugsnag a try. In the past I've used the exception_notification gem, and have even rolled my own solution for handling exceptions, but for this new app, I thought I'd do something new. I'm a big fan of leveraging existing "solutions" for common tasks, so I can focus on the core business of the application (see Buy-vs-build for an early stage startup).

bugsnag has nice documentation to help you get started. What I wanted to do was just catch every exception that's generated by the app, regardless of specific type. As the app matures and we decide what we want to do in a particular case, we'll handle each exception type differently, but for now, a global catch-all will suffice:

# config/initializers/bugsnag.rb
Bugsnag.configure do |config|
  config.api_key = "API_KEY"
  config.ignore_classes = []
end

# app/controllers/application.rb
class ApplicationController < ActionController::Base
  rescue_from Exception, :with => :handle_public_excepton
  
  protected

  def handle_public_excepton(exception)
    render :template => "shared/exception"
  end
end

So we catch every exception, and display a generic error page that lives in app/views/shared/. This wound up being trivial but it was a good excuse to dig into Rails 3 internals and learn about how exceptions are handled within the framework.

See Also

rescue_from dispatching

rails/activesupport/lib/active_support/rescuable.rb