Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle throwing in controller action in log subscriber #41223

Merged
merged 1 commit into from Jan 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions actionpack/CHANGELOG.md
@@ -1,5 +1,9 @@
## 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`.

*Alex Robbin*
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/log_subscriber.rb
Expand Up @@ -23,7 +23,7 @@ def process_action(event)
additions = ActionController::Base.log_process_action(payload)
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)
end

Expand Down
12 changes: 12 additions & 0 deletions actionpack/test/controller/log_subscriber_test.rb
Expand Up @@ -64,6 +64,10 @@ def with_fragment_cache_unless_with_true_condition
render inline: "<%= cache_unless(true, 'foo') { 'bar' } %>"
end

def with_throw
throw :halt
end

def with_exception
raise Exception
end
Expand Down Expand Up @@ -190,6 +194,14 @@ def test_process_action_with_view_runtime
assert_match(/Completed 200 OK in \d+ms/, logs[1])
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
begin
get :with_exception
Expand Down