class Airbrake::Rack::Middleware

Airbrake Rack middleware for Rails and Sinatra applications (or any other Rack-compliant app). Any errors raised by the upstream application will be delivered to Airbrake and re-raised.

The middleware automatically sends information about the framework that uses it (name and version).

Public Class Methods

new(app, notifier_name = :default) click to toggle source
# File lib/airbrake/rack/middleware.rb, line 11
def initialize(app, notifier_name = :default)
  @app = app
  @notifier_name = notifier_name
end

Public Instance Methods

call(env) click to toggle source

Rescues any exceptions, sends them to Airbrake and re-raises the exception. @param [Hash] env the Rack environment

# File lib/airbrake/rack/middleware.rb, line 20
def call(env)
  # rubocop:disable Lint/RescueException
  begin
    response = @app.call(env)
  rescue Exception => ex
    notify_airbrake(ex, env)
    raise ex
  end
  # rubocop:enable Lint/RescueException

  exception = framework_exception(env)
  notify_airbrake(exception, env) if exception

  response
end

Private Instance Methods

framework_exception(env) click to toggle source

Web framework middlewares often store rescued exceptions inside the Rack env, but Rack doesn't have a standard key for it:

# File lib/airbrake/rack/middleware.rb, line 52
def framework_exception(env)
  env['action_dispatch.exception'] ||
    env['sinatra.error'] ||
    env['rack.exception']
end
notify_airbrake(exception, env) click to toggle source
# File lib/airbrake/rack/middleware.rb, line 38
def notify_airbrake(exception, env)
  notice = NoticeBuilder.new(env, @notifier_name).build_notice(exception)
  return unless notice

  Airbrake.notify(notice, {}, @notifier_name)
end