Sha256: 5d47242e6920b921911adc0b1e690db3c9544835413907a90ccc6d7d3bb5f5cc

Contents?: true

Size: 1.14 KB

Versions: 13

Compression:

Stored size: 1.14 KB

Contents

require 'rack/utils'

module Deas

  class ShowExceptions

    def initialize(app)
      @app = app
    end

    # The Rack call interface. The receiver acts as a prototype and runs
    # each request in a clone object unless the +rack.run_once+ variable is
    # set in the environment. Ripped from:
    # http://github.com/rtomayko/rack-cache/blob/master/lib/rack/cache/context.rb
    def call(env)
      if env['rack.run_once']
        call! env
      else
        clone.call! env
      end
    end

    # The real Rack call interface.
    def call!(env)
      status, headers, body = @app.call(env)
      if error = env['deas.error']
        error_body = Body.new(error)

        headers['Content-Length'] = error_body.size.to_s
        headers['Content-Type']   = error_body.mime_type.to_s
        body = [error_body.content]
      end
      [status, headers, body]
    end

    class Body
      attr_reader :content, :size, :mime_type

      def initialize(e)
        @content   = "#{e.class}: #{e.message}\n#{(e.backtrace || []).join("\n")}"
        @size      = Rack::Utils.bytesize(@content)
        @mime_type = "text/plain"
      end
    end

  end

end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
deas-0.43.5 lib/deas/show_exceptions.rb
deas-0.43.4 lib/deas/show_exceptions.rb
deas-0.43.3 lib/deas/show_exceptions.rb
deas-0.43.2 lib/deas/show_exceptions.rb
deas-0.43.1 lib/deas/show_exceptions.rb
deas-0.43.0 lib/deas/show_exceptions.rb
deas-0.42.0 lib/deas/show_exceptions.rb
deas-0.41.0 lib/deas/show_exceptions.rb
deas-0.40.0 lib/deas/show_exceptions.rb
deas-0.39.2 lib/deas/show_exceptions.rb
deas-0.39.1 lib/deas/show_exceptions.rb
deas-0.39.0 lib/deas/show_exceptions.rb
deas-0.38.0 lib/deas/show_exceptions.rb