aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch/middleware/public_exceptions.rb
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2011-12-16 09:29:37 +0100
committerJosé Valim <jose.valim@gmail.com>2011-12-16 10:45:59 +0100
commitdeef8dd68241cdb9826a2293bced5e2257ccea90 (patch)
tree08240789fde8372129737f2ef0f710ff9b9a9a44 /actionpack/lib/action_dispatch/middleware/public_exceptions.rb
parentd1425725673b0172d6561adebaed017b51db9333 (diff)
downloadrails-deef8dd68241cdb9826a2293bced5e2257ccea90.tar.gz
rails-deef8dd68241cdb9826a2293bced5e2257ccea90.tar.bz2
rails-deef8dd68241cdb9826a2293bced5e2257ccea90.zip
Extract the rendering of public exceptions pages into a Rack app.
Diffstat (limited to 'actionpack/lib/action_dispatch/middleware/public_exceptions.rb')
-rw-r--r--actionpack/lib/action_dispatch/middleware/public_exceptions.rb30
1 files changed, 30 insertions, 0 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/public_exceptions.rb b/actionpack/lib/action_dispatch/middleware/public_exceptions.rb
new file mode 100644
index 0000000000..6600d6232e
--- /dev/null
+++ b/actionpack/lib/action_dispatch/middleware/public_exceptions.rb
@@ -0,0 +1,30 @@
+module ActionDispatch
+ # A simple Rack application that renders exceptions in the given public path.
+ class PublicExceptions
+ attr_accessor :public_path
+
+ def initialize(public_path)
+ @public_path = public_path
+ end
+
+ def call(env)
+ status = env["PATH_INFO"][1..-1]
+ locale_path = "#{public_path}/#{status}.#{I18n.locale}.html" if I18n.locale
+ path = "#{public_path}/#{status}.html"
+
+ if locale_path && File.exist?(locale_path)
+ render(status, File.read(locale_path))
+ elsif File.exist?(path)
+ render(status, File.read(path))
+ else
+ render(status, '')
+ end
+ end
+
+ private
+
+ def render(status, body)
+ [status, {'Content-Type' => "text/html; charset=#{Response.default_charset}", 'Content-Length' => body.bytesize.to_s}, [body]]
+ end
+ end
+end \ No newline at end of file