Sha256: 26fefad5ed83d14d368f1432722d51d5c68aa31aa999faf3d64bee664114c9bf

Contents?: true

Size: 1.18 KB

Versions: 31

Compression:

Stored size: 1.18 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['sinatra.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
      def initialize(e)
        @content ||= "#{e.class}: #{e.message}\n#{(e.backtrace || []).join("\n")}"
      end

      def size
        @size ||= Rack::Utils.bytesize(self.content)
      end

      def mime_type
        @mime_type ||= "text/plain"
      end
    end

  end

end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
deas-0.37.0 lib/deas/show_exceptions.rb
deas-0.36.0 lib/deas/show_exceptions.rb
deas-0.35.0 lib/deas/show_exceptions.rb
deas-0.34.0 lib/deas/show_exceptions.rb
deas-0.33.0 lib/deas/show_exceptions.rb
deas-0.32.0 lib/deas/show_exceptions.rb
deas-0.31.0 lib/deas/show_exceptions.rb
deas-0.30.0 lib/deas/show_exceptions.rb
deas-0.29.0 lib/deas/show_exceptions.rb
deas-0.28.0 lib/deas/show_exceptions.rb
deas-0.27.0 lib/deas/show_exceptions.rb
deas-0.26.0 lib/deas/show_exceptions.rb
deas-0.25.0 lib/deas/show_exceptions.rb
deas-0.24.1 lib/deas/show_exceptions.rb
deas-0.24.0 lib/deas/show_exceptions.rb
deas-0.23.4 lib/deas/show_exceptions.rb
deas-0.23.3 lib/deas/show_exceptions.rb
deas-0.23.2 lib/deas/show_exceptions.rb
deas-0.23.1 lib/deas/show_exceptions.rb
deas-0.23.0 lib/deas/show_exceptions.rb