Sha256: c2f11f54bcbd7225c78770366e0b145f8906ae76ee8fdfb6417ba5bdb60fedec

Contents?: true

Size: 1.44 KB

Versions: 10

Compression:

Stored size: 1.44 KB

Contents

module Honeybadger
  # Middleware for Rack applications. Any errors raised by the upstream
  # application will be delivered to Honeybadger and re-raised.
  #
  # Synopsis:
  #
  #   require 'rack'
  #   require 'honeybadger'
  #
  #   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
  #   run app
  #
  # Use a standard Honeybadger.configure call to configure your api key.
  class Rack
    def initialize(app)
      @app = app
      Honeybadger.configuration.framework = "Rack: #{::Rack.release}"
    end

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

    def notify_honeybadger(exception,env)
      Honeybadger.notify_or_ignore(exception, :rack_env => env) unless ignored_user_agent?(env)
    end

    def call(env)
      begin
        response = @app.call(env)
      rescue Exception => raised
        env['honeybadger.error_id'] = notify_honeybadger(raised, env)
        raise
      ensure
        Honeybadger.context.clear!
      end

      framework_exception = env['rack.exception'] || env['sinatra.error']
      if framework_exception
        env['honeybadger.error_id'] = notify_honeybadger(framework_exception, env)
      end

      response
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
honeybadger-1.13.2 lib/honeybadger/rack.rb
honeybadger-1.13.1 lib/honeybadger/rack.rb
honeybadger-1.13.0 lib/honeybadger/rack.rb
honeybadger-1.11.2 lib/honeybadger/rack.rb
honeybadger-1.11.1 lib/honeybadger/rack.rb
honeybadger-1.11.0 lib/honeybadger/rack.rb
honeybadger-1.11.0.beta4 lib/honeybadger/rack.rb
honeybadger-1.11.0.beta3 lib/honeybadger/rack.rb
honeybadger-1.11.0.beta2 lib/honeybadger/rack.rb
honeybadger-1.11.0.beta1 lib/honeybadger/rack.rb