@@ -11,7 +11,7 @@ module ActiveSupport
11
11
#
12
12
# If an error is raised, it will be reported and swallowed.
13
13
#
14
- # Alternatively if you want to report the error but not swallow it, you can use ` record`
14
+ # Alternatively if you want to report the error but not swallow it, you can use + record+
15
15
#
16
16
# Rails.error.record do
17
17
# do_something!
@@ -31,10 +31,11 @@ module ActiveSupport
31
31
# +severity+ can be one of +:error+, +:warning+ or +:info+. Handled errors default to the +:warning+
32
32
# severity, and unhandled ones to +error+.
33
33
#
34
- # Both `handle` and `record` pass through the return value from the block. In the special case of `handle` handling an
35
- # error, a fallback value can be provided that will be returned:
34
+ # Both +handle+ and +record+ pass through the return value from the block. In the case of +handle+
35
+ # rescuing an error, a fallback can be provided. The fallback must be a callable whose result will
36
+ # be returned when the block raises and is handled:
36
37
#
37
- # user = Rails.error.handle(fallback: User.anonymous) do
38
+ # user = Rails.error.handle(fallback: -> { User.anonymous } ) do
38
39
# User.find_by(params)
39
40
# end
40
41
class ErrorReporter
@@ -57,7 +58,7 @@ def handle(error_class = StandardError, severity: :warning, context: {}, fallbac
57
58
yield
58
59
rescue error_class => error
59
60
report ( error , handled : true , severity : severity , context : context )
60
- fallback
61
+ fallback . call if fallback
61
62
end
62
63
63
64
def record ( error_class = StandardError , severity : :error , context : { } )
@@ -90,7 +91,7 @@ def set_context(...)
90
91
91
92
# When the block based +handle+ and +record+ methods are not suitable, you can directly use +report+
92
93
#
93
- # Rails.error.report(error, handled: true)
94
+ # Rails.error.report(error, handled: true)
94
95
def report ( error , handled :, severity : handled ? :warning : :error , context : { } )
95
96
unless SEVERITIES . include? ( severity )
96
97
raise ArgumentError , "severity must be one of #{ SEVERITIES . map ( &:inspect ) . join ( ", " ) } , got: #{ severity . inspect } "
0 commit comments