class Airbrake::Rack::Middleware

For Rails apps the middleware collects route performance statistics.
uses it (name and version).
The middleware automatically sends information about the framework that
delivered to Airbrake and re-raised.
Rack-compliant app). Any errors raised by the upstream application will be
Airbrake Rack middleware for Rails and Sinatra applications (or any other

def before_call(env)

def before_call(env)
  # Rails hooks such as ActionControllerRouteSubscriber rely on this.
  RequestStore[:routes] = {}
  RequestStore[:request] = find_request(env)
end

def call(env)

Other tags:
    See: https://github.com/airbrake/airbrake/issues/904 -

Parameters:
  • env (Hash) -- the Rack environment
def call(env)
  dup.call!(env)
end

def call!(env)

Parameters:
  • env (Hash) -- the Rack environment
def call!(env)
  before_call(env)
  begin
    response = @app.call(env)
  rescue Exception => ex # rubocop:disable Lint/RescueException
    notify_airbrake(ex)
    raise ex
  end
  exception = framework_exception(env)
  notify_airbrake(exception) if exception
  response
ensure
  # Clear routes for the next request.
  RequestStore.clear
end

def find_request(env)

def find_request(env)
  if defined?(ActionDispatch::Request)
    ActionDispatch::Request.new(env)
  elsif defined?(Sinatra::Request)
    Sinatra::Request.new(env)
  else
    ::Rack::Request.new(env)
  end
end

def framework_exception(env)

- Goliath uses rack.exception: https://goo.gl/i7e1nA
- Sinatra uses sinatra.error: https://goo.gl/LLkVL9
- Rails uses action_dispatch.exception: https://goo.gl/Kd694n

Rack env, but Rack doesn't have a standard key for it:
Web framework middlewares often store rescued exceptions inside the
def framework_exception(env)
  env['action_dispatch.exception'] ||
    env['sinatra.error'] ||
    env['rack.exception']
end

def initialize(app)

def initialize(app)
  @app = app
end

def notify_airbrake(exception)

def notify_airbrake(exception)
  notice = Airbrake.build_notice(exception)
  return unless notice
  # ActionDispatch::Request correctly captures server port when using SSL:
  # See: https://github.com/airbrake/airbrake/issues/802
  notice.stash[:rack_request] = RequestStore[:request]
  Airbrake.notify(notice)
end