Sha256: 5391b438f6774bd6f339393bd48abb5d5747b1b55f76631409c1e301291915b6

Contents?: true

Size: 1.96 KB

Versions: 15

Compression:

Stored size: 1.96 KB

Contents

require 'rack'
require 'honeybadger'
require 'forwardable'

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'
    #
    #   Honeybadger.configure do |config|
    #     config.api_key = 'my_api_key'
    #   end
    #
    #   app = Rack::Builder.app do
    #     run lambda { |env| raise "Rack down" }
    #   end
    #
    #   use Honeybadger::Rack::ErrorNotifier
    #   run app
    #
    # Use a standard Honeybadger.configure call to configure your api key.
    class ErrorNotifier
      extend Forwardable

      def initialize(app, config)
        @app = app
        @config = config
      end

      def call(env)
        config.with_request(::Rack::Request.new(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
        Honeybadger.context.clear!
      end

      private

      attr_reader :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)
        Honeybadger.notify_or_ignore(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

15 entries across 15 versions & 1 rubygems

Version Path
honeybadger-2.0.1 lib/honeybadger/rack/error_notifier.rb
honeybadger-2.0.0 lib/honeybadger/rack/error_notifier.rb
honeybadger-2.0.0.beta.13 lib/honeybadger/rack/error_notifier.rb
honeybadger-2.0.0.beta.12 lib/honeybadger/rack/error_notifier.rb
honeybadger-2.0.0.beta.11 lib/honeybadger/rack/error_notifier.rb
honeybadger-2.0.0.beta.10 lib/honeybadger/rack/error_notifier.rb
honeybadger-2.0.0.beta.9 lib/honeybadger/rack/error_notifier.rb
honeybadger-2.0.0.beta.8 lib/honeybadger/rack/error_notifier.rb
honeybadger-2.0.0.beta.7 lib/honeybadger/rack/error_notifier.rb
honeybadger-2.0.0.beta.6 lib/honeybadger/rack/error_notifier.rb
honeybadger-2.0.0.beta.5 lib/honeybadger/rack/error_notifier.rb
honeybadger-2.0.0.beta.4 lib/honeybadger/rack/error_notifier.rb
honeybadger-2.0.0.beta.3 lib/honeybadger/rack/error_notifier.rb
honeybadger-2.0.0.beta.2 lib/honeybadger/rack/error_notifier.rb
honeybadger-2.0.0.beta.1 lib/honeybadger/rack/error_notifier.rb