Merge pull request #41223 from janko/controller-throw-log-subscriber

Handle throwing in controller action in log subscriber
This commit is contained in:
Ryuta Kamizono 2021-01-24 18:15:47 +09:00 committed by GitHub
commit b32823a7ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 1 deletions

View File

@ -1,5 +1,9 @@
## Unreleased ## Unreleased
* Fix error in `ActionController::LogSubscriber` that would happen when throwing inside a controller action.
*Janko Marohnić*
* Change the request method to a `GET` when passing failed requests down to `config.exceptions_app`. * Change the request method to a `GET` when passing failed requests down to `config.exceptions_app`.
*Alex Robbin* *Alex Robbin*

View File

@ -23,7 +23,7 @@ module ActionController
additions = ActionController::Base.log_process_action(payload) additions = ActionController::Base.log_process_action(payload)
status = payload[:status] status = payload[:status]
if status.nil? && (exception_class_name = payload[:exception].first) if status.nil? && (exception_class_name = payload[:exception]&.first)
status = ActionDispatch::ExceptionWrapper.status_code_for_exception(exception_class_name) status = ActionDispatch::ExceptionWrapper.status_code_for_exception(exception_class_name)
end end

View File

@ -64,6 +64,10 @@ module Another
render inline: "<%= cache_unless(true, 'foo') { 'bar' } %>" render inline: "<%= cache_unless(true, 'foo') { 'bar' } %>"
end end
def with_throw
throw :halt
end
def with_exception def with_exception
raise Exception raise Exception
end end
@ -190,6 +194,14 @@ class ACLogSubscriberTest < ActionController::TestCase
assert_match(/Completed 200 OK in \d+ms/, logs[1]) assert_match(/Completed 200 OK in \d+ms/, logs[1])
end end
def test_process_action_with_throw
catch(:halt) do
get :with_throw
wait
end
assert_match(/Completed in \d+ms/, logs[1])
end
def test_append_info_to_payload_is_called_even_with_exception def test_append_info_to_payload_is_called_even_with_exception
begin begin
get :with_exception get :with_exception