Sha256: 175fca106465beae0309f318d0cd14b379d2f9a1340e9bbcf983e710785b9fd4

Contents?: true

Size: 1.62 KB

Versions: 5

Compression:

Stored size: 1.62 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)
        @app = app
      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).build_notice(exception)
        Airbrake.notify(notice)
      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

5 entries across 5 versions & 1 rubygems

Version Path
airbrake-5.2.3 lib/airbrake/rack/middleware.rb
airbrake-5.2.2 lib/airbrake/rack/middleware.rb
airbrake-5.2.1 lib/airbrake/rack/middleware.rb
airbrake-5.2.0 lib/airbrake/rack/middleware.rb
airbrake-5.1.0 lib/airbrake/rack/middleware.rb