Skip to content

Using Airbrake with plain Ruby

jgarber edited this page Nov 12, 2012 · 2 revisions

You can also pass a hash to Airbrake.notify method and store whatever you want, not just an exception. And you can also use it anywhere, not just in controllers:

Airbrake.configure do |config|
  config.api_key = '1234deadbeef...'
end

begin
  params = {
     # params that you pass to a method that can throw an exception
  }
  my_unpredicable_method(params)
rescue => e
  Airbrake.notify_or_ignore(
   e,
   :parameters    => params,
   :cgi_data      => ENV
  )
end

While in your controllers you use the notify_airbrake method, anywhere else in your code, use Airbrake.notify. Airbrake will get all the information about the error itself. As for a hash, these are the keys you should pass:

  • :error_class - Use this to group similar errors together. When Airbrake catches an exception it sends the class name of that exception object.
  • :error_message - This is the title of the error you see in the errors list. For exceptions it is "#{exception.class.name}: #{exception.message}"
  • :parameters - While there are several ways to send additional data to Airbrake, passing a Hash as :parameters as in the example above is the most common use case. When Airbrake catches an exception in a controller, the actual HTTP client request parameters are sent using this key.

Airbrake merges the hash you pass with these default options:

    {
      :api_key       => Airbrake.api_key,
      :error_message => 'Notification',
      :backtrace     => caller,
      :parameters    => {},
      :session       => {}
    }

You can override any of those parameters.

Sending shell environment variables

One common request we see is to send shell environment variables along with manual exception notification. We recommend sending them along with CGI data (:cgi_data).

See Airbrake::Notice#initialize in lib/airbrake/notice.rb for more details.