class Honeybadger::Rack::ErrorNotifier

Middleware for Rack applications. Any errors raised by the upstream application will be delivered to Honeybadger and re-raised.

@example require 'honeybadger/rack/error_notifier'

app = Rack::Builder.app do
  run lambda { |env| raise "Rack down" }
end

use Honeybadger::Rack::ErrorNotifier

run app

Public Class Methods

new(app, agent = nil) click to toggle source
# File lib/honeybadger/rack/error_notifier.rb, line 24
def initialize(app, agent = nil)
  @app = app
  @agent = agent.kind_of?(Agent) && agent
end

Public Instance Methods

call(env) click to toggle source
# File lib/honeybadger/rack/error_notifier.rb, line 29
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.clear!
end

Private Instance Methods

agent() click to toggle source
# File lib/honeybadger/rack/error_notifier.rb, line 55
def agent
  @agent || Honeybadger::Agent.instance
end
framework_exception(env) click to toggle source
# File lib/honeybadger/rack/error_notifier.rb, line 86
def framework_exception(env)
  env['action_dispatch.exception'] || env['rack.exception'] ||
    env['sinatra.error'] || env['honeybadger.exception']
end
ignored_user_agent?(env) click to toggle source
# File lib/honeybadger/rack/error_notifier.rb, line 59
def ignored_user_agent?(env)
  true if config[:'exceptions.ignored_user_agents'].
    flatten.
    any? { |ua| ua === env['HTTP_USER_AGENT'] }
end
notify_honeybadger(exception, env) click to toggle source
# File lib/honeybadger/rack/error_notifier.rb, line 65
def notify_honeybadger(exception, env)
  return if ignored_user_agent?(env)

  if config[:'breadcrumbs.enabled']
    # Drop the last breadcrumb only if the message contains the error class name
    agent.breadcrumbs.drop_previous_breadcrumb_if do |bc|
      bc.category == "log" && bc.message.include?(exception.class.to_s)
    end

    agent.add_breadcrumb(
      exception.class,
      metadata: {
        exception_message: exception.message
      },
      category: "error"
    )
  end

  agent.notify(exception)
end