Class: Rack::ShowStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/showstatus.rb

Overview

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.

Constant Summary

TEMPLATE =

adapted from Django <djangoproject.com> Copyright © 2005, the Lawrence Journal-World Used under the modified BSD license: www.xfree86.org/3.3.6/COPYRIGHT2.html#5

"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html lang=\"en\">\n<head>\n  <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n  <title><%=h message %> at <%=h req.script_name + req.path_info %></title>\n  <meta name=\"robots\" content=\"NONE,NOARCHIVE\" />\n  <style type=\"text/css\">\n    html * { padding:0; margin:0; }\n    body * { padding:10px 20px; }\n    body * * { padding:0; }\n    body { font:small sans-serif; background:#eee; }\n    body>div { border-bottom:1px solid #ddd; }\n    h1 { font-weight:normal; margin-bottom:.4em; }\n    h1 span { font-size:60%; color:#666; font-weight:normal; }\n    table { border:none; border-collapse: collapse; width:100%; }\n    td, th { vertical-align:top; padding:2px 3px; }\n    th { width:12em; text-align:right; color:#666; padding-right:.5em; }\n    #info { background:#f6f6f6; }\n    #info ol { margin: 0.5em 4em; }\n    #info ol li { font-family: monospace; }\n    #summary { background: #ffc; }\n    #explanation { background:#eee; border-bottom: 0px none; }\n  </style>\n</head>\n<body>\n  <div id=\"summary\">\n    <h1><%=h message %> <span>(<%= status.to_i %>)</span></h1>\n    <table class=\"meta\">\n      <tr>\n        <th>Request Method:</th>\n        <td><%=h req.request_method %></td>\n      </tr>\n      <tr>\n        <th>Request URL:</th>\n      <td><%=h req.url %></td>\n      </tr>\n    </table>\n  </div>\n  <div id=\"info\">\n    <p><%= detail %></p>\n  </div>\n\n  <div id=\"explanation\">\n    <p>\n    You're seeing this error because you use <code>Rack::ShowStatus</code>.\n    </p>\n  </div>\n</body>\n</html>\n"

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ShowStatus



14
15
16
17
# File 'lib/rack/showstatus.rb', line 14

def initialize(app)
  @app = app
  @template = ERB.new(TEMPLATE)
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rack/showstatus.rb', line 19

def call(env)
  status, headers, body = @app.call(env)
  headers = Utils::HeaderHash.new(headers)
  empty = headers['Content-Length'].to_i <= 0

  # client or server error, or explicit message
  if (status.to_i >= 400 && empty) || env["rack.showstatus.detail"]
    req = Rack::Request.new(env)
    message = Rack::Utils::HTTP_STATUS_CODES[status.to_i] || status.to_s
    detail = env["rack.showstatus.detail"] || message
    body = @template.result(binding)
    size = Rack::Utils.bytesize(body)
    [status, headers.merge("Content-Type" => "text/html", "Content-Length" => size.to_s), [body]]
  else
    [status, headers, body]
  end
end

#h(obj) ⇒ Object

:nodoc:



37
38
39
40
41
42
43
44
# File 'lib/rack/showstatus.rb', line 37

def h(obj)                  # :nodoc:
  case obj
  when String
    Utils.escape_html(obj)
  else
    Utils.escape_html(obj.inspect)
  end
end