Class | Rack::ShowStatus |
In: |
lib/rack/showstatus.rb
|
Parent: | Object |
Rack::ShowStatus catches all empty responses the app it wraps and replaces them with a site explaining the error.
Additional details can be put into rack.showstatus.detail and will be shown as HTML. If such details exist, the error page is always rendered, even if the reply was not empty.
# File lib/rack/showstatus.rb, line 14 14: def initialize(app) 15: @app = app 16: @template = ERB.new(TEMPLATE) 17: end
# File lib/rack/showstatus.rb, line 19 19: def call(env) 20: status, headers, body = @app.call(env) 21: headers = Utils::HeaderHash.new(headers) 22: empty = headers['Content-Length'].to_i <= 0 23: 24: # client or server error, or explicit message 25: if (status.to_i >= 400 && empty) || env["rack.showstatus.detail"] 26: req = Rack::Request.new(env) 27: message = Rack::Utils::HTTP_STATUS_CODES[status.to_i] || status.to_s 28: detail = env["rack.showstatus.detail"] || message 29: body = @template.result(binding) 30: size = body.respond_to?(:bytesize) ? body.bytesize : body.size 31: [status, headers.merge("Content-Type" => "text/html", "Content-Length" => size.to_s), [body]] 32: else 33: [status, headers, body] 34: end 35: end