提交 b4359bc7 编写于 作者: J José Valim

Allow rescue responses to be configured through a railtie.

上级 1e51cd95
## Rails 3.2.0 (unreleased) ## ## Rails 3.2.0 (unreleased) ##
* Allow rescue responses to be configured through a railtie as in `config.action_dispatch.rescue_responses`. Please look at ActiveRecord::Railtie for an example. *José Valim*
* Assets should use the request protocol by default or default to relative if no request is available *Jonathan del Strother* * Assets should use the request protocol by default or default to relative if no request is available *Jonathan del Strother*
* Log "Filter chain halted as CALLBACKNAME rendered or redirected" every time a before callback halts *José Valim* * Log "Filter chain halted as CALLBACKNAME rendered or redirected" every time a before callback halts *José Valim*
......
...@@ -12,26 +12,22 @@ class ShowExceptions ...@@ -12,26 +12,22 @@ class ShowExceptions
cattr_accessor :rescue_responses cattr_accessor :rescue_responses
@@rescue_responses = Hash.new(:internal_server_error) @@rescue_responses = Hash.new(:internal_server_error)
@@rescue_responses.update({ @@rescue_responses.merge!(
'ActionController::RoutingError' => :not_found, 'ActionController::RoutingError' => :not_found,
'AbstractController::ActionNotFound' => :not_found, 'AbstractController::ActionNotFound' => :not_found,
'ActiveRecord::RecordNotFound' => :not_found,
'ActiveRecord::StaleObjectError' => :conflict,
'ActiveRecord::RecordInvalid' => :unprocessable_entity,
'ActiveRecord::RecordNotSaved' => :unprocessable_entity,
'ActionController::MethodNotAllowed' => :method_not_allowed, 'ActionController::MethodNotAllowed' => :method_not_allowed,
'ActionController::NotImplemented' => :not_implemented, 'ActionController::NotImplemented' => :not_implemented,
'ActionController::InvalidAuthenticityToken' => :unprocessable_entity 'ActionController::InvalidAuthenticityToken' => :unprocessable_entity
}) )
cattr_accessor :rescue_templates cattr_accessor :rescue_templates
@@rescue_templates = Hash.new('diagnostics') @@rescue_templates = Hash.new('diagnostics')
@@rescue_templates.update({ @@rescue_templates.merge!(
'ActionView::MissingTemplate' => 'missing_template', 'ActionView::MissingTemplate' => 'missing_template',
'ActionController::RoutingError' => 'routing_error', 'ActionController::RoutingError' => 'routing_error',
'AbstractController::ActionNotFound' => 'unknown_action', 'AbstractController::ActionNotFound' => 'unknown_action',
'ActionView::Template::Error' => 'template_error' 'ActionView::Template::Error' => 'template_error'
}) )
FAILSAFE_RESPONSE = [500, {'Content-Type' => 'text/html'}, FAILSAFE_RESPONSE = [500, {'Content-Type' => 'text/html'},
["<html><body><h1>500 Internal Server Error</h1>" << ["<html><body><h1>500 Internal Server Error</h1>" <<
......
...@@ -9,11 +9,22 @@ class Railtie < Rails::Railtie ...@@ -9,11 +9,22 @@ class Railtie < Rails::Railtie
config.action_dispatch.best_standards_support = true config.action_dispatch.best_standards_support = true
config.action_dispatch.tld_length = 1 config.action_dispatch.tld_length = 1
config.action_dispatch.ignore_accept_header = false config.action_dispatch.ignore_accept_header = false
config.action_dispatch.rack_cache = {:metastore => "rails:/", :entitystore => "rails:/", :verbose => true} config.action_dispatch.rescue_templates = { }
config.action_dispatch.rescue_responses = { }
config.action_dispatch.rack_cache = {
:metastore => "rails:/",
:entitystore => "rails:/",
:verbose => true
}
initializer "action_dispatch.configure" do |app| initializer "action_dispatch.configure" do |app|
ActionDispatch::Http::URL.tld_length = app.config.action_dispatch.tld_length ActionDispatch::Http::URL.tld_length = app.config.action_dispatch.tld_length
ActionDispatch::Request.ignore_accept_header = app.config.action_dispatch.ignore_accept_header ActionDispatch::Request.ignore_accept_header = app.config.action_dispatch.ignore_accept_header
ActionDispatch::ShowExceptions.rescue_responses.merge!(config.action_dispatch.rescue_responses)
ActionDispatch::ShowExceptions.rescue_templates.merge!(config.action_dispatch.rescue_templates)
config.action_dispatch.always_write_cookie = Rails.env.development? if config.action_dispatch.always_write_cookie.nil? config.action_dispatch.always_write_cookie = Rails.env.development? if config.action_dispatch.always_write_cookie.nil?
ActionDispatch::Cookies::CookieJar.always_write_cookie = config.action_dispatch.always_write_cookie ActionDispatch::Cookies::CookieJar.always_write_cookie = config.action_dispatch.always_write_cookie
end end
......
...@@ -22,6 +22,13 @@ class Railtie < Rails::Railtie ...@@ -22,6 +22,13 @@ class Railtie < Rails::Railtie
config.app_middleware.insert_after "::ActionDispatch::Callbacks", config.app_middleware.insert_after "::ActionDispatch::Callbacks",
"ActiveRecord::ConnectionAdapters::ConnectionManagement" "ActiveRecord::ConnectionAdapters::ConnectionManagement"
config.action_dispatch.rescue_responses.merge!(
'ActiveRecord::RecordNotFound' => :not_found,
'ActiveRecord::StaleObjectError' => :conflict,
'ActiveRecord::RecordInvalid' => :unprocessable_entity,
'ActiveRecord::RecordNotSaved' => :unprocessable_entity
)
rake_tasks do rake_tasks do
load "active_record/railties/databases.rake" load "active_record/railties/databases.rake"
end end
......
...@@ -16,6 +16,35 @@ def teardown ...@@ -16,6 +16,35 @@ def teardown
teardown_app teardown_app
end end
test "show exceptions middleware filter backtrace before logging" do
my_middleware = Struct.new(:app) do
def call(env)
raise "Failure"
end
end
app.config.middleware.use my_middleware
stringio = StringIO.new
Rails.logger = Logger.new(stringio)
get "/"
assert_no_match(/action_dispatch/, stringio.string)
end
test "renders active record exceptions as 404" do
my_middleware = Struct.new(:app) do
def call(env)
raise ActiveRecord::RecordNotFound
end
end
app.config.middleware.use my_middleware
get "/"
assert_equal 404, last_response.status
end
test "unspecified route when set action_dispatch.show_exceptions to false" do test "unspecified route when set action_dispatch.show_exceptions to false" do
app.config.action_dispatch.show_exceptions = false app.config.action_dispatch.show_exceptions = false
......
...@@ -104,7 +104,7 @@ def app ...@@ -104,7 +104,7 @@ def app
assert !middleware.include?("ActionDispatch::Static") assert !middleware.include?("ActionDispatch::Static")
end end
test "includes show exceptions even action_dispatch.show_exceptions is disabled" do test "includes show exceptions even if action_dispatch.show_exceptions is disabled" do
add_to_config "config.action_dispatch.show_exceptions = false" add_to_config "config.action_dispatch.show_exceptions = false"
boot! boot!
assert middleware.include?("ActionDispatch::ShowExceptions") assert middleware.include?("ActionDispatch::ShowExceptions")
...@@ -191,26 +191,6 @@ def index ...@@ -191,26 +191,6 @@ def index
assert_equal nil, last_response.headers["Etag"] assert_equal nil, last_response.headers["Etag"]
end end
# Show exceptions middleware
test "show exceptions middleware filter backtrace before logging" do
my_middleware = Struct.new(:app) do
def call(env)
raise "Failure"
end
end
make_basic_app do |app|
app.config.middleware.use my_middleware
end
stringio = StringIO.new
Rails.logger = Logger.new(stringio)
env = Rack::MockRequest.env_for("/")
Rails.application.call(env)
assert_no_match(/action_dispatch/, stringio.string)
end
private private
def boot! def boot!
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册