Sha256: 63638d7003c97c1a4a3205096617003d86d4c58c7eb51f94c58ebf0837407212

Contents?: true

Size: 1.19 KB

Versions: 5

Compression:

Stored size: 1.19 KB

Contents

require 'forwardable'

module Honeybadger
  module Rack
    # Public: 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 : Honeybadger::Agent.instance
      end

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

      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

      private

      attr_reader :agent
      def_delegator :agent, :config
      def_delegator :config, :logger
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
honeybadger-3.2.0 lib/honeybadger/rack/user_informer.rb
honeybadger-3.2.0.beta1 lib/honeybadger/rack/user_informer.rb
honeybadger-3.1.2 lib/honeybadger/rack/user_informer.rb
honeybadger-3.1.1 lib/honeybadger/rack/user_informer.rb
honeybadger-3.1.0 lib/honeybadger/rack/user_informer.rb