Sha256: 0630d5536d9ec6dbe017c9c39c52b5995614e9dac2c593020c5018bc5bdced4f

Contents?: true

Size: 1.85 KB

Versions: 12

Compression:

Stored size: 1.85 KB

Contents

require 'forwardable'
require 'rack/request'

require 'honeybadger/ruby'

module Honeybadger
  module Rack
    # Public: Middleware for Rack applications. Any errors raised by the upstream
    # application will be delivered to Honeybadger and re-raised.
    #
    # Examples:
    #
    #   require 'honeybadger/rack/error_notifier'
    #
    #   app = Rack::Builder.app do
    #     run lambda { |env| raise "Rack down" }
    #   end
    #
    #   use Honeybadger::Rack::ErrorNotifier
    #
    #   run app
    class ErrorNotifier
      extend Forwardable

      def initialize(app, agent = nil)
        @app = app
        @agent = agent.kind_of?(Agent) ? agent : Honeybadger::Agent.instance
      end

      def call(env)
        agent.with_rack_env(env) do
          begin
            env['honeybadger.config'] = config
            response = @app.call(env)
          rescue Exception => raised
            env['honeybadger.error_id'] = notify_honeybadger(raised, env)
            raise
          end

          framework_exception = framework_exception(env)
          if framework_exception
            env['honeybadger.error_id'] = notify_honeybadger(framework_exception, env)
          end

          response
        end
      ensure
        agent.context.clear!
      end

      private

      attr_reader :agent
      def_delegator :agent, :config
      def_delegator :config, :logger

      def ignored_user_agent?(env)
        true if config[:'exceptions.ignored_user_agents'].
          flatten.
          any? { |ua| ua === env['HTTP_USER_AGENT'] }
      end

      def notify_honeybadger(exception, env)
        return if ignored_user_agent?(env)
        agent.notify(exception)
      end

      def framework_exception(env)
        env['action_dispatch.exception'] || env['rack.exception'] ||
          env['sinatra.error'] || env['honeybadger.exception']
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
honeybadger-3.1.2 lib/honeybadger/rack/error_notifier.rb
honeybadger-3.1.1 lib/honeybadger/rack/error_notifier.rb
honeybadger-3.1.0 lib/honeybadger/rack/error_notifier.rb
honeybadger-3.0.2 lib/honeybadger/rack/error_notifier.rb
honeybadger-3.0.1 lib/honeybadger/rack/error_notifier.rb
honeybadger-3.0.0 lib/honeybadger/rack/error_notifier.rb
honeybadger-3.0.0.beta6 lib/honeybadger/rack/error_notifier.rb
honeybadger-3.0.0.beta5 lib/honeybadger/rack/error_notifier.rb
honeybadger-3.0.0.beta4 lib/honeybadger/rack/error_notifier.rb
honeybadger-3.0.0.beta3 lib/honeybadger/rack/error_notifier.rb
honeybadger-3.0.0.beta2 lib/honeybadger/rack/error_notifier.rb
honeybadger-3.0.0.beta1 lib/honeybadger/rack/error_notifier.rb