Rails Exception Tracking

Typical installation time: 2 minutes

Hi there! You've found Honeybadger's guide to Rails exception tracking. Once installed, Honeybadger will automatically report exceptions wherever they may happen:

  • During a web request
  • In a background job
  • In a rake task
  • When a process crashes (at_exit)

If you're new to Honeybadger, read our Getting Started guide to become familiar with our Ruby gem. For a refresher on working with exceptions in Ruby, check out the Honeybadger guide to Ruby exceptions.

On this page:

Installation

Using the Honeybadger gem with Rails

The first step is to add the honeybadger gem to your Gemfile:

gem 'honeybadger'

Tell bundler to install:

$ bundle install

Next, you'll set the API key for this project.

$ bundle exec honeybadger install [YOUR API KEY HERE]

This will do three things:

  1. Generate a config/honeybadger.yml file. If you don't like config files, you can place your API key in the $HONEYBADGER_API_KEY environment variable.
  2. If Capistrano is installed, we'll add a require statement to Capfile.
  3. Send a test exception to your Honeybadger project.

Assuming the test completed successfully: you're done!

Custom Error Pages

The Honeybadger gem has a few special tags that it looks for whenever you render an error page. These can be used to display extra information about the error, or to ask the user for information about how they triggered the error.

Displaying Error ID

When an error is sent to Honeybadger, our API returns a unique UUID for the occurrence within your project. This UUID can be automatically displayed for reference on Rails error pages (e.g. public/500.html) or any rack output by including the Honeybadger::UserInformer middleware.

To include the error id, simply place this magic HTML comment on your error page:

<!-- HONEYBADGER ERROR -->

By default, we will replace this tag with:

Honeybadger Error 

Where `is the UUID. You can customize this output by overriding theuser_informer.info` option in your honeybadger.yml file (you can also enabled/disable the middleware):

user_informer:
  enabled: true
  info: "Error ID: "

You can use that UUID to load the error at the site by going to  https://app.honeybadger.io/notice/some-uuid-goes-here.

Collecting User Feedback

When an error is sent to Honeybadger, an HTML form can be generated so users can fill out relevant information that led up to that error. Feedback responses are displayed inline in the comments section on the fault detail page.

To include a user feedback form on your error page, simply add this magic HTML comment:

<!-- HONEYBADGER FEEDBACK -->

You can change the text displayed in the form via the Rails internationalization system. Here's an example:

# config/locales/en.yml
en:
  honeybadger:
    feedback:
      heading: "Care to help us fix this?"
      explanation: "Any information you can provide will help us fix the problem."
      submit: "Send"
      thanks: "Thanks for the feedback!"
      labels:
        name: "Your name"
        email: "Your email address"
        comment: "Comment (required)"

Identifying Users

If you're using the devise or the warden gems for user authentication, then we already associate errors with the current user.

For other authentication systems (or to customize the user values), add the following before_action to your ApplicationController:

before_action do
  Honeybadger.context({
    user_id: current_user.id,
    user_email: current_user.email
  })
end