Extract the rendering of public exceptions pages into a Rack app.

This commit is contained in:
José Valim 2011-12-16 09:29:37 +01:00
parent d142572567
commit deef8dd682
7 changed files with 53 additions and 45 deletions

@ -56,6 +56,7 @@ module ActionDispatch
autoload :Flash
autoload :Head
autoload :ParamsParser
autoload :PublicExceptions
autoload :Reloader
autoload :RemoteIp
autoload :Rescue

@ -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

@ -6,8 +6,6 @@ module ActionDispatch
# This middleware rescues any exception returned by the application
# and wraps them in a format for the end user.
class ShowExceptions
RESCUES_TEMPLATE_PATH = File.join(File.dirname(__FILE__), 'templates')
FAILSAFE_RESPONSE = [500, {'Content-Type' => 'text/html'},
["<html><body><h1>500 Internal Server Error</h1>" <<
"If you are the administrator of this website, then please read this web " <<
@ -28,9 +26,19 @@ def rescue_templates
end
end
def initialize(app, consider_all_requests_local = nil)
ActiveSupport::Deprecation.warn "Passing consider_all_requests_local option to ActionDispatch::ShowExceptions middleware no longer works" unless consider_all_requests_local.nil?
def initialize(app, exceptions_app = nil)
if [true, false].include?(exceptions_app)
ActiveSupport::Deprecation.warn "Passing consider_all_requests_local option to ActionDispatch::ShowExceptions middleware no longer works"
exceptions_app = nil
end
if exceptions_app.nil?
raise ArgumentError, "You need to pass an exceptions_app when initializing ActionDispatch::ShowExceptions. " \
"In case you want to render pages from a public path, you can use ActionDispatch::PublicExceptions.new('path/to/public')"
end
@app = app
@exceptions_app = exceptions_app
end
def call(env)
@ -40,7 +48,7 @@ def call(env)
raise exception if env['action_dispatch.show_exceptions'] == false
end
response || render_exception_with_failsafe(env, exception)
response || render_exception(env, exception)
end
private
@ -50,37 +58,14 @@ def call(env)
def status_code(*)
end
def render_exception_with_failsafe(env, exception)
render_exception(env, exception)
def render_exception(env, exception)
wrapper = ExceptionWrapper.new(env, exception)
env["action_dispatch.exception"] = wrapper.exception
env["PATH_INFO"] = "/#{wrapper.status_code}"
@exceptions_app.call(env)
rescue Exception => failsafe_error
$stderr.puts "Error during failsafe response: #{failsafe_error}\n #{failsafe_error.backtrace * "\n "}"
FAILSAFE_RESPONSE
end
def render_exception(env, exception)
wrapper = ExceptionWrapper.new(env, exception)
status = wrapper.status_code
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
def render(status, body)
[status, {'Content-Type' => "text/html; charset=#{Response.default_charset}", 'Content-Length' => body.bytesize.to_s}, [body]]
end
# TODO: Make this a middleware initialization parameter once
# we removed the second option (which is deprecated)
def public_path
defined?(Rails.public_path) ? Rails.public_path : 'public_path'
end
end
end

@ -174,7 +174,7 @@ class ActionDispatch::IntegrationTest < ActiveSupport::TestCase
def self.build_app(routes = nil)
RoutedRackApp.new(routes || ActionDispatch::Routing::RouteSet.new) do |middleware|
middleware.use "ActionDispatch::ShowExceptions"
middleware.use "ActionDispatch::ShowExceptions", ActionDispatch::PublicExceptions.new("#{FIXTURE_LOAD_PATH}/public")
middleware.use "ActionDispatch::DebugExceptions"
middleware.use "ActionDispatch::Callbacks"
middleware.use "ActionDispatch::ParamsParser"
@ -337,14 +337,6 @@ def to_s
end
module ActionDispatch
class ShowExceptions
private
remove_method :public_path
def public_path
"#{FIXTURE_LOAD_PATH}/public"
end
end
class DebugExceptions
private
remove_method :stderr_logger

@ -2,7 +2,7 @@
module ShowExceptions
class ShowExceptionsController < ActionController::Base
use ActionDispatch::ShowExceptions
use ActionDispatch::ShowExceptions, ActionDispatch::PublicExceptions.new("#{FIXTURE_LOAD_PATH}/public")
use ActionDispatch::DebugExceptions
before_filter :only => :another_boom do

@ -18,7 +18,7 @@ def call(env)
end
end
ProductionApp = ActionDispatch::ShowExceptions.new(Boomer.new)
ProductionApp = ActionDispatch::ShowExceptions.new(Boomer.new, ActionDispatch::PublicExceptions.new("#{FIXTURE_LOAD_PATH}/public"))
test 'skip diagnosis if not showing exceptions' do
@app = ProductionApp

@ -244,7 +244,7 @@ def default_middleware_stack
middleware.use ::Rack::MethodOverride
middleware.use ::ActionDispatch::RequestId
middleware.use ::Rails::Rack::Logger, config.log_tags # must come after Rack::MethodOverride to properly log overridden methods
middleware.use ::ActionDispatch::ShowExceptions
middleware.use ::ActionDispatch::ShowExceptions, ActionDispatch::PublicExceptions.new(Rails.public_path)
middleware.use ::ActionDispatch::DebugExceptions
middleware.use ::ActionDispatch::RemoteIp, config.action_dispatch.ip_spoofing_check, config.action_dispatch.trusted_proxies