Skip to content

Commit 5a3db3d

Browse files
swebbbyroot
andcommitted
Log when rescue_from handles an exception
In development when a `before_action` renders or redirects this error message appears on the logs: > Filter chain halted as :before_action rendered or redirected I find this message very helpful. However, if a before_action raises an error that is handled by a rescue_from block, nothing is logged indicating an error occurred. This PR adds a log message indicating an exception was rescued, and displays where it was originally raised. For example: > rescue_from handled BaseController::YourError (Oh no!) - > app/controllers/home_controller.rb:9:in 'HomeController#authenticate' Co-Authored-By: Jean Boussier <[email protected]>
1 parent 4a38d3c commit 5a3db3d

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

actionpack/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Produce a log when `rescue_from` is invoked.
2+
3+
*Steven Webb*, *Jean Boussier*
4+
15
* Allow hosts redirects from `hosts` Rails configuration
26

37
```ruby

actionpack/lib/action_controller/log_subscriber.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ def halted_callback(event)
4949
end
5050
subscribe_log_level :halted_callback, :info
5151

52+
# Manually subscribed below
53+
def rescue_from_callback(event)
54+
exception = event.payload[:exception]
55+
info { "rescue_from handled #{exception.class} (#{exception.message}) - #{exception.backtrace.first.delete_prefix("#{Rails.root}/")}" }
56+
end
57+
subscribe_log_level :rescue_from_callback, :info
58+
5259
def send_file(event)
5360
info { "Sent file #{event.payload[:path]} (#{event.duration.round(1)}ms)" }
5461
end

actionpack/test/controller/log_subscriber_test.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ def with_throw
7777
end
7878

7979
def with_exception
80-
raise Exception
80+
raise Exception, "Oopsie"
8181
end
8282

8383
def with_rescued_exception
84-
raise SpecialException
84+
raise SpecialException, "Oops"
8585
end
8686

8787
def with_action_not_found
@@ -154,6 +154,13 @@ def test_halted_callback
154154
assert_equal "Filter chain halted as :redirector rendered or redirected", logs.third
155155
end
156156

157+
def test_rescue_from_callback
158+
get :with_rescued_exception
159+
wait
160+
assert_equal 3, logs.size
161+
assert_match "rescue_from handled Another::LogSubscribersController::SpecialException", logs.second
162+
end
163+
157164
def test_process_action
158165
get :show
159166
wait
@@ -406,7 +413,7 @@ def test_process_action_with_rescued_exception_includes_http_status_code
406413
get :with_rescued_exception
407414
wait
408415

409-
assert_equal 2, logs.size
416+
assert_equal 3, logs.size
410417
assert_match(/Completed 406/, logs.last)
411418
end
412419

activesupport/lib/active_support/rescuable.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def rescue_with_handler(exception, object: self, visited_exceptions: [])
9292

9393
if handler = handler_for_rescue(exception, object: object)
9494
handler.call exception
95+
ActiveSupport::Notifications.instrument("rescue_from_callback.action_controller", exception: exception)
9596
exception
9697
elsif exception
9798
if visited_exceptions.include?(exception.cause)

0 commit comments

Comments
 (0)