Sha256: e8bb61d253ec8423768d1a384cbcee1d4876deaae754f40b9b29841d990273b2

Contents?: true

Size: 1.21 KB

Versions: 4

Compression:

Stored size: 1.21 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
      def initialize(error)
        @error = error
      end

      def content
        @content ||= "#{@error.class}: #{@error.message}\n#{@error.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

4 entries across 4 versions & 1 rubygems

Version Path
deas-0.14.0 lib/deas/show_exceptions.rb
deas-0.13.1 lib/deas/show_exceptions.rb
deas-0.13.0 lib/deas/show_exceptions.rb
deas-0.12.0 lib/deas/show_exceptions.rb