Permalink
Browse files

Add Rack::ShowStatus, a filter to generate common error messages

darcs-hash:20070329142432-4fc50-136d3e1563b5beb09a290a8bacc21cc29b2ed9f5.gz
  • Loading branch information...
chneukirchen committed Mar 29, 2007
1 parent d110c39 commit 4abfcfcb9a87aefdb269a51516c4637129a013e1
Showing with 171 additions and 0 deletions.
  1. +1 −0 lib/rack.rb
  2. +108 −0 lib/rack/showstatus.rb
  3. +62 −0 test/spec_rack_showstatus.rb
View
@@ -29,6 +29,7 @@ def self.version
autoload :Recursive, "rack/recursive"
autoload :Reloader, "rack/reloader"
autoload :ShowExceptions, "rack/showexceptions"
+ autoload :ShowStatus, "rack/showstatus"
autoload :Static, "rack/static"
autoload :URLMap, "rack/urlmap"
autoload :Utils, "rack/utils"
View
@@ -0,0 +1,108 @@
+require 'erb'
+require 'rack/request'
+require 'rack/utils'
+
+module Rack
+ # 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.
+
+ class ShowStatus
+ def initialize(app)
+ @app = app
+ @template = ERB.new(TEMPLATE)
+ end
+
+ def call(env)
+ status, headers, body = @app.call(env)
+
+ # client or server error, or explicit message
+ if status.to_i >= 400 || env["rack.showstatus.detail"]
+ empty = true
+ body.each { empty = false; break }
+ if 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
+ [status, {"Content-Type" => "text/html"}, [@template.result(binding)]]
+ else
+ [status, headers, body]
+ end
+ else
+ [status, headers, body]
+ end
+ end
+
+ def h(obj)
+ case obj
+ when String
+ Utils.escape_html(obj)
+ else
+ Utils.escape_html(obj.inspect)
+ end
+ end
+
+ # :stopdoc:
+
+# adapted from Django <djangoproject.com>
+# Copyright (c) 2005, the Lawrence Journal-World
+# Used under the modified BSD license:
+# http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5
+TEMPLATE = <<'HTML'
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html lang="en">
+<head>
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
+ <title><%=h message %> at <%=h req.script_name + req.path_info %></title>
+ <meta name="robots" content="NONE,NOARCHIVE" />
+ <style type="text/css">
+ html * { padding:0; margin:0; }
+ body * { padding:10px 20px; }
+ body * * { padding:0; }
+ body { font:small sans-serif; background:#eee; }
+ body>div { border-bottom:1px solid #ddd; }
+ h1 { font-weight:normal; margin-bottom:.4em; }
+ h1 span { font-size:60%; color:#666; font-weight:normal; }
+ table { border:none; border-collapse: collapse; width:100%; }
+ td, th { vertical-align:top; padding:2px 3px; }
+ th { width:12em; text-align:right; color:#666; padding-right:.5em; }
+ #info { background:#f6f6f6; }
+ #info ol { margin: 0.5em 4em; }
+ #info ol li { font-family: monospace; }
+ #summary { background: #ffc; }
+ #explanation { background:#eee; border-bottom: 0px none; }
+ </style>
+</head>
+<body>
+ <div id="summary">
+ <h1><%=h message %> <span>(<%= status.to_i %>)</span></h1>
+ <table class="meta">
+ <tr>
+ <th>Request Method:</th>
+ <td><%=h req.request_method %></td>
+ </tr>
+ <tr>
+ <th>Request URL:</th>
+ <td><%=h req.url %></td>
+ </tr>
+ </table>
+ </div>
+ <div id="info">
+ <p><%= detail %></p>
+ </div>
+
+ <div id="explanation">
+ <p>
+ You're seeing this error because you use <code>Rack::ShowStatus</code>.
+ </p>
+ </div>
+</body>
+</html>
+HTML
+
+ # :startdoc:
+ end
+end
@@ -0,0 +1,62 @@
+require 'test/spec'
+
+require 'rack/showstatus'
+require 'rack/mock'
+
+context "Rack::ShowStatus" do
+ specify "should provide a default status message" do
+ req = Rack::MockRequest.new(Rack::ShowStatus.new(lambda { |env|
+ [404, {"Content-Type" => "text/plain"}, []]
+ }))
+
+ res = req.get("/", :lint => true)
+ res.should.be.not_found
+ res.should.be.not.empty
+
+ res["Content-Type"].should.equal("text/html")
+ res.should =~ /404/
+ res.should =~ /Not Found/
+ end
+
+ specify "should let the app provide additional information" do
+ req = Rack::MockRequest.new(Rack::ShowStatus.new(lambda { |env|
+ env["rack.showstatus.detail"] = "gone too meta."
+ [404, {"Content-Type" => "text/plain"}, []]
+ }))
+
+ res = req.get("/", :lint => true)
+ res.should.be.not_found
+ res.should.be.not.empty
+
+ res["Content-Type"].should.equal("text/html")
+ res.should =~ /404/
+ res.should =~ /Not Found/
+ res.should =~ /too meta/
+ end
+
+ specify "should not replace existing messages" do
+ req = Rack::MockRequest.new(Rack::ShowStatus.new(lambda { |env|
+ [404, {"Content-Type" => "text/plain"}, ["foo!"]]
+ }))
+ res = req.get("/", :lint => true)
+ res.should.be.not_found
+
+ res.body.should == "foo!"
+ end
+
+ specify "should replace existing messages if there is detail" do
+ req = Rack::MockRequest.new(Rack::ShowStatus.new(lambda { |env|
+ env["rack.showstatus.detail"] = "gone too meta."
+ [404, {"Content-Type" => "text/plain"}, ["foo!"]]
+ }))
+
+ res = req.get("/", :lint => true)
+ res.should.be.not_found
+ res.should.be.not.empty
+
+ res["Content-Type"].should.equal("text/html")
+ res.should =~ /404/
+ res.should =~ /too meta/
+ res.body.should.not =~ /foo/
+ end
+end

0 comments on commit 4abfcfc

Please sign in to comment.