@@ -19,17 +19,24 @@ module ActiveSupport
19
19
#
20
20
# Both methods can be restricted to only handle a specific exception class
21
21
#
22
- # maybe_tags = Rails.error.handle(Redis::BaseError) { redis.get("tags") }
22
+ # maybe_tags = Rails.error.handle(Redis::BaseError) { redis.get("tags") }
23
23
#
24
24
# You can also pass some extra context information that may be used by the error subscribers:
25
25
#
26
- # Rails.error.handle(context: { section: "admin" }) do
27
- # # ...
28
- # end
26
+ # Rails.error.handle(context: { section: "admin" }) do
27
+ # # ...
28
+ # end
29
29
#
30
30
# Additionally a +severity+ can be passed along to communicate how important the error report is.
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
+ #
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:
36
+ #
37
+ # user = Rails.error.handle(fallback: User.anonymous) do
38
+ # User.find_by(params)
39
+ # end
33
40
class ErrorReporter
34
41
SEVERITIES = %i( error warning info )
35
42
@@ -42,15 +49,15 @@ def initialize(*subscribers, logger: nil)
42
49
43
50
# Report any unhandled exception, and swallow it.
44
51
#
45
- # Rails.error.handle do
46
- # 1 + '1'
47
- # end
52
+ # Rails.error.handle do
53
+ # 1 + '1'
54
+ # end
48
55
#
49
- def handle ( error_class = StandardError , severity : :warning , context : { } )
56
+ def handle ( error_class = StandardError , severity : :warning , context : { } , fallback : nil )
50
57
yield
51
58
rescue error_class => error
52
59
report ( error , handled : true , severity : severity , context : context )
53
- nil
60
+ fallback
54
61
end
55
62
56
63
def record ( error_class = StandardError , severity : :error , context : { } )
0 commit comments