EventMachine integration guide

Add Bugsnag to your EventMachine projects.

Installation

Bugsnag’s Ruby gem can be installed using Bundler by adding the gem to your Gemfile:

gem 'bugsnag-em'

Don’t forget to run bundle install after updating your Gemfile.

Basic configuration

To identify your app in the Bugsnag dashboard, you’ll need to configure your Bugsnag API key. You can find your API key when creating a project in your Bugsnag dashboard, or later from your project settings page.

To set your API key in your app, add the following code to your entrypoint:

require 'bugsnag'

Bugsnag.configure do |config|
  config.api_key = 'YOUR_API_KEY_HERE'
end

Alternatively, you can set the BUGSNAG_API_KEY environment variable.

If you’d like to configure Bugsnag further, check out the configuration options reference.

Reporting unhandled exceptions

To automatically capture unhandled exceptions in your EventMachine applications, you’ll need to implement EventMachine.error_handler:

EventMachine.error_handler{|e|
  Bugsnag.notify(e)
}

If you’d like more fine-grained error handling, you can use the errback function, for example:

EventMachine::run do
  server = EventMachine::start_server('0.0.0.0', PORT, MyServer)
  server.errback {
    EM.defer do
      Bugsnag.notify(RuntimeError.new('Something bad happened'))
    end
  }
end

For this to work, include Deferrable in your MyServer, then whenever you want to raise an error, call fail.

Reporting handled exceptions

Reporting handled exceptions can be accomplished as follows:

begin
  raise 'Something went wrong!'
rescue => exception
  Bugsnag.notify(exception)
end

Adding diagnostics or adjusting severity

It can often be helpful to adjust the severity or attach custom diagnostics to handled exceptions. For more information, see reporting handled errors.

Identifying users

In order to correlate errors with customer reports, or to see a list of users who experienced each error, it is helpful to capture and display user information on your Bugsnag dashboard.

You can set this information using a Bugsnag.with block as follows:

Bugsnag.with(user: {id: current_user.id, email: current_user.email}) do
  EM::next_tick do
    raise 'oops'
  end
end

For more information, see reporting handled errors.

Sending diagnostic data

In order to quickly reproduce and fix errors, it is often helpful to send additional application-specific diagnostic data to Bugsnag. This can be accomplished using the Bugsnag.with call:

Bugsnag.with(user_id: '123') do
  # Your code here:
  http = EM::HttpRequest.new('http://google.com/').get
  http.errback{ raise 'oops' }
  ...
end

Tracking deploys

By sending your source revision or application version to us when you deploy a new version of your app, you’ll be able to see which deploy each error was introduced in, or seen in.

Find information on how to track deployment using Capistrano in our how to track deploys using Capistrano

If you are using a deployment system other than Capistrano, see our deploy tracking guide.

Catching JavaScript errors

Catch and report errors in your client-side JavaScript with the Web browser JavaScript guide