Sha256: e32e8fcb5d1f4681296dc17ed080ae0fe97cd20eb303eb88d2bba88c3ea40329

Contents?: true

Size: 1.62 KB

Versions: 17

Compression:

Stored size: 1.62 KB

Contents

module Airbrake
  module Rails
    # Rack middleware for Rails applications. Any errors raised by the upstream
    # application will be delivered to Airbrake and re-raised.
    #
    class Middleware
      def initialize(app)
        @app = app
      end

      def call(env)
        begin
          response = @app.call(env)
        rescue Exception => exception
          env['airbrake.error_id'] = notify_airbrake(env, exception)
          raise exception
        end

        if framework_exception = env["action_dispatch.exception"]
          env["airbrake.error_id"] = notify_airbrake(env, framework_exception)
        end

        response
      end

      private

      def controller(env)
        env["action_controller.instance"]
      end

      def after_airbrake_handler(env, exception)
        if controller(env).respond_to?(:rescue_action_in_public_without_airbrake)
          controller(env).rescue_action_in_public_without_airbrake(exception)
        end
      end

      def notify_airbrake(env, exception)
        unless ignored_user_agent? env
          error_id = Airbrake.notify_or_ignore(exception, request_data(env))
          after_airbrake_handler(env, exception)
          error_id
        end
      end

      def request_data(env)
        if controller(env).respond_to?(:airbrake_request_data)
          controller(env).airbrake_request_data
        else
          {:rack_env => env}
        end
      end

      def ignored_user_agent?(env)
        true if Airbrake.
          configuration.
          ignore_user_agent.
          flatten.
          any? { |ua| ua === env['HTTP_USER_AGENT'] }
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
airbrakeV4rails5-4.3.8 lib/airbrake/rails/middleware.rb
airbrake-4.3.8 lib/airbrake/rails/middleware.rb
airbrake-4.3.7 lib/airbrake/rails/middleware.rb
airbrake-4.3.6 lib/airbrake/rails/middleware.rb
airbrake-4.3.5 lib/airbrake/rails/middleware.rb
airbrake-4.3.4 lib/airbrake/rails/middleware.rb
airbrake-4.3.3 lib/airbrake/rails/middleware.rb
airbrake-4.3.2 lib/airbrake/rails/middleware.rb
airbrake-4.3.1 lib/airbrake/rails/middleware.rb
airbrake-4.3.0 lib/airbrake/rails/middleware.rb
airbrake-4.2.1 lib/airbrake/rails/middleware.rb
airbrake-4.2.0 lib/airbrake/rails/middleware.rb
airbrake-4.1.0 lib/airbrake/rails/middleware.rb
airbrake-4.0.0 lib/airbrake/rails/middleware.rb
airbrake-3.2.1 lib/airbrake/rails/middleware.rb
airbrake-3.2.0 lib/airbrake/rails/middleware.rb
airbrake-3.1.17 lib/airbrake/rails/middleware.rb