From 370dcd99b7635f6a5075da73de27c6da59a8553b Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Sun, 16 Sep 2012 21:48:24 +0200 Subject: [PATCH 1/2] log 404 status when ActiveRecord::RecordNotFound was raised (#7646) --- actionpack/CHANGELOG.md | 4 ++++ .../lib/action_controller/log_subscriber.rb | 3 ++- .../middleware/exception_wrapper.rb | 6 +++++- actionpack/test/controller/log_subscriber_test.rb | 15 +++++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index f9af8934fd5..ea4217c0d50 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,9 @@ ## Rails 4.0.0 (unreleased) ## +* Fix #7646, the log now displays the correct status code when an exception is raised. + + *Yves Senn* + * Allow pass couple extensions to ActionView::Template.register_template_handler call. *Tima Maslyuchenko* * Sprockets integration has been extracted from Action Pack and the `sprockets-rails` diff --git a/actionpack/lib/action_controller/log_subscriber.rb b/actionpack/lib/action_controller/log_subscriber.rb index a7c0e971e7f..f41d1bb4b90 100644 --- a/actionpack/lib/action_controller/log_subscriber.rb +++ b/actionpack/lib/action_controller/log_subscriber.rb @@ -19,7 +19,8 @@ module ActionController status = payload[:status] if status.nil? && payload[:exception].present? - status = ActionDispatch::ExceptionWrapper.new({}, payload[:exception]).status_code + exception_class_name = payload[:exception].first + status = ActionDispatch::ExceptionWrapper.status_code_for_exception(exception_class_name) end message = "Completed #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]} in %.0fms" % event.duration message << " (#{additions.join(" | ")})" unless additions.blank? diff --git a/actionpack/lib/action_dispatch/middleware/exception_wrapper.rb b/actionpack/lib/action_dispatch/middleware/exception_wrapper.rb index 7349b578d28..ae38c56a670 100644 --- a/actionpack/lib/action_dispatch/middleware/exception_wrapper.rb +++ b/actionpack/lib/action_dispatch/middleware/exception_wrapper.rb @@ -37,7 +37,7 @@ module ActionDispatch end def status_code - Rack::Utils.status_code(@@rescue_responses[@exception.class.name]) + self.class.status_code_for_exception(@exception.class.name) end def application_trace @@ -52,6 +52,10 @@ module ActionDispatch clean_backtrace(:all) end + def self.status_code_for_exception(class_name) + Rack::Utils.status_code(@@rescue_responses[class_name]) + end + private def original_exception(exception) diff --git a/actionpack/test/controller/log_subscriber_test.rb b/actionpack/test/controller/log_subscriber_test.rb index 700fd788faa..a72b6dde1a9 100644 --- a/actionpack/test/controller/log_subscriber_test.rb +++ b/actionpack/test/controller/log_subscriber_test.rb @@ -54,6 +54,10 @@ module Another def with_rescued_exception raise SpecialException end + + def with_action_not_found + raise AbstractController::ActionNotFound + end end end @@ -225,6 +229,17 @@ class ACLogSubscriberTest < ActionController::TestCase assert_match(/Completed 406/, logs.last) end + def test_process_action_with_with_action_not_found_logs_404 + begin + get :with_action_not_found + wait + rescue AbstractController::ActionNotFound + end + + assert_equal 2, logs.size + assert_match(/Completed 404/, logs.last) + end + def logs @logs ||= @logger.logged(:info) end From dadfa9ab32669cda34f01a1cc06afbe97c8a0f79 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Mon, 17 Sep 2012 18:17:37 +0200 Subject: [PATCH 2/2] cleanup, remove whitespace from CHANGELOG --- actionpack/CHANGELOG.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index ea4217c0d50..39105c5ca35 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -6,16 +6,16 @@ * Allow pass couple extensions to ActionView::Template.register_template_handler call. *Tima Maslyuchenko* -* Sprockets integration has been extracted from Action Pack and the `sprockets-rails` +* Sprockets integration has been extracted from Action Pack and the `sprockets-rails` gem should be added to Gemfile (under the assets group) in order to use Rails asset - pipeline in future versions of Rails. + pipeline in future versions of Rails. *Guillermo Iguaran* -* `ActionDispatch::Session::MemCacheStore` now uses `dalli` instead of the deprecated - `memcache-client` gem. As side effect the autoloading of unloaded classes objects - saved as values in session isn't supported anymore when mem_cache session store is - used, this can have an impact in apps only when config.cache_classes is false. +* `ActionDispatch::Session::MemCacheStore` now uses `dalli` instead of the deprecated + `memcache-client` gem. As side effect the autoloading of unloaded classes objects + saved as values in session isn't supported anymore when mem_cache session store is + used, this can have an impact in apps only when config.cache_classes is false. *Arun Agrawal + Guillermo Iguaran*