class Honeybadger::Rack::UserInformer

Middleware for Rack applications. Adds an error ID to the Rack response when an error has occurred.

Public Class Methods

new(app, agent = nil) click to toggle source
# File lib/honeybadger/rack/user_informer.rb, line 10
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/user_informer.rb, line 19
def call(env)
  return @app.call(env) unless config[:'user_informer.enabled']
  status, headers, body = @app.call(env)
  if env['honeybadger.error_id']
    new_body = []
    replace  = replacement(env['honeybadger.error_id'])
    body.each do |chunk|
      new_body << chunk.gsub("<!-- HONEYBADGER ERROR -->", replace)
    end
    body.close if body.respond_to?(:close)
    headers['Content-Length'] = new_body.reduce(0) { |a,e| a += e.bytesize }.to_s
    body = new_body
  end
  [status, headers, body]
end
replacement(with) click to toggle source
# File lib/honeybadger/rack/user_informer.rb, line 15
def replacement(with)
  config[:'user_informer.info'].gsub(/\{\{\s*error_id\s*\}\}/, with.to_s)
end

Private Instance Methods

agent() click to toggle source
# File lib/honeybadger/rack/user_informer.rb, line 40
def agent
  @agent || Honeybadger::Agent.instance
end