Skip to content

Commit

Permalink
Merge pull request #41223 from janko/controller-throw-log-subscriber
Browse files Browse the repository at this point in the history
Handle throwing in controller action in log subscriber
  • Loading branch information
kamipo committed Jan 24, 2021
1 parent ab23ee6 commit 04d6ac8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions actionpack/CHANGELOG.md
@@ -1,3 +1,7 @@
* Fix error in `ActionController::LogSubscriber` that would happen when throwing inside a controller action.

*Janko Marohnić*

* Fix `fixture_file_upload` deprecation when `file_fixture_path` is a relative path.

*Eugene Kenny*
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

0 comments on commit 04d6ac8

Please sign in to comment.