Sha256: cd54bf69608cebb3a7ae0fc533ac336373b9bc9c842d3e81bea064fed57c73d1

Contents?: true

Size: 1.13 KB

Versions: 30

Compression:

Stored size: 1.13 KB

Contents

require 'forwardable'

module Honeybadger
  module Rack
    # Middleware for Rack applications. Adds an error ID to the Rack response
    # when an error has occurred.
    class UserInformer
      extend Forwardable

      def initialize(app, agent = nil)
        @app = app
        @agent = agent.kind_of?(Agent) && agent
      end

      def replacement(with)
        config[:'user_informer.info'].gsub(/\{\{\s*error_id\s*\}\}/, with.to_s)
      end

      def call(env)
        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

      private

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

      def agent
        @agent || Honeybadger::Agent.instance
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
honeybadger-5.24.0 lib/honeybadger/rack/user_informer.rb
honeybadger-5.23.0 lib/honeybadger/rack/user_informer.rb
honeybadger-5.22.0 lib/honeybadger/rack/user_informer.rb
honeybadger-5.21.0 lib/honeybadger/rack/user_informer.rb
honeybadger-5.20.1 lib/honeybadger/rack/user_informer.rb
honeybadger-5.20.0 lib/honeybadger/rack/user_informer.rb
honeybadger-5.19.2 lib/honeybadger/rack/user_informer.rb
honeybadger-5.19.1 lib/honeybadger/rack/user_informer.rb
honeybadger-5.19.0 lib/honeybadger/rack/user_informer.rb
honeybadger-5.18.0 lib/honeybadger/rack/user_informer.rb
honeybadger-5.17.0 lib/honeybadger/rack/user_informer.rb
honeybadger-5.16.0 lib/honeybadger/rack/user_informer.rb
honeybadger-5.15.6 lib/honeybadger/rack/user_informer.rb
honeybadger-5.15.5 lib/honeybadger/rack/user_informer.rb
honeybadger-5.15.4 lib/honeybadger/rack/user_informer.rb
honeybadger-5.15.3 lib/honeybadger/rack/user_informer.rb
honeybadger-5.15.2 lib/honeybadger/rack/user_informer.rb
honeybadger-5.15.1 lib/honeybadger/rack/user_informer.rb
honeybadger-5.15.0 lib/honeybadger/rack/user_informer.rb
honeybadger-5.14.2 lib/honeybadger/rack/user_informer.rb