Sha256: 5e5e72eea0ce31de8d47a51741b5d2703d9952c93466ea7cdfad0d92afb54e60

Contents?: true

Size: 1.75 KB

Versions: 3

Compression:

Stored size: 1.75 KB

Contents

module Airbrake
  module Rack
    ##
    # 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).
    class Middleware
      def initialize(app, notifier_name = :default)
        @app = app
        @notifier_name = notifier_name
      end

      ##
      # Rescues any exceptions, sends them to Airbrake and re-raises the
      # exception.
      # @param [Hash] env the Rack environment
      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

      def notify_airbrake(exception, env)
        notice = NoticeBuilder.new(env, @notifier_name).build_notice(exception)
        return unless notice

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

      ##
      # Web framework middlewares often store rescued exceptions inside the
      # Rack env, but Rack doesn't have a standard key for it:
      #
      # - Rails uses action_dispatch.exception: https://goo.gl/Kd694n
      # - Sinatra uses sinatra.error: https://goo.gl/LLkVL9
      # - Goliath uses rack.exception: https://goo.gl/i7e1nA
      def framework_exception(env)
        env['action_dispatch.exception'] ||
          env['sinatra.error'] ||
          env['rack.exception']
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
dfg-airbrake-5.6.2 lib/airbrake/rack/middleware.rb
airbrake-5.6.1 lib/airbrake/rack/middleware.rb
airbrake-5.6.0 lib/airbrake/rack/middleware.rb