mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Allow a fallback value to be returned from Rails.error.handle
This commit is contained in:
parent
89b1cc4818
commit
ac751edab3
2 changed files with 47 additions and 9 deletions
|
@ -19,17 +19,24 @@ module ActiveSupport
|
||||||
#
|
#
|
||||||
# Both methods can be restricted to only handle a specific exception class
|
# Both methods can be restricted to only handle a specific exception class
|
||||||
#
|
#
|
||||||
# maybe_tags = Rails.error.handle(Redis::BaseError) { redis.get("tags") }
|
# maybe_tags = Rails.error.handle(Redis::BaseError) { redis.get("tags") }
|
||||||
#
|
#
|
||||||
# You can also pass some extra context information that may be used by the error subscribers:
|
# You can also pass some extra context information that may be used by the error subscribers:
|
||||||
#
|
#
|
||||||
# Rails.error.handle(context: { section: "admin" }) do
|
# Rails.error.handle(context: { section: "admin" }) do
|
||||||
# # ...
|
# # ...
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# Additionally a +severity+ can be passed along to communicate how important the error report is.
|
# Additionally a +severity+ can be passed along to communicate how important the error report is.
|
||||||
# +severity+ can be one of +:error+, +:warning+ or +:info+. Handled errors default to the +:warning+
|
# +severity+ can be one of +:error+, +:warning+ or +:info+. Handled errors default to the +:warning+
|
||||||
# severity, and unhandled ones to +error+.
|
# severity, and unhandled ones to +error+.
|
||||||
|
#
|
||||||
|
# Both `handle` and `record` pass through the return value from the block. In the special case of `handle` handling an
|
||||||
|
# error, a fallback value can be provided that will be returned:
|
||||||
|
#
|
||||||
|
# user = Rails.error.handle(fallback: User.anonymous) do
|
||||||
|
# User.find_by(params)
|
||||||
|
# end
|
||||||
class ErrorReporter
|
class ErrorReporter
|
||||||
SEVERITIES = %i(error warning info)
|
SEVERITIES = %i(error warning info)
|
||||||
|
|
||||||
|
@ -42,15 +49,15 @@ module ActiveSupport
|
||||||
|
|
||||||
# Report any unhandled exception, and swallow it.
|
# Report any unhandled exception, and swallow it.
|
||||||
#
|
#
|
||||||
# Rails.error.handle do
|
# Rails.error.handle do
|
||||||
# 1 + '1'
|
# 1 + '1'
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
def handle(error_class = StandardError, severity: :warning, context: {})
|
def handle(error_class = StandardError, severity: :warning, context: {}, fallback: nil)
|
||||||
yield
|
yield
|
||||||
rescue error_class => error
|
rescue error_class => error
|
||||||
report(error, handled: true, severity: severity, context: context)
|
report(error, handled: true, severity: severity, context: context)
|
||||||
nil
|
fallback
|
||||||
end
|
end
|
||||||
|
|
||||||
def record(error_class = StandardError, severity: :error, context: {})
|
def record(error_class = StandardError, severity: :error, context: {})
|
||||||
|
|
|
@ -58,6 +58,30 @@ class ErrorReporterTest < ActiveSupport::TestCase
|
||||||
assert_equal [], @subscriber.events
|
assert_equal [], @subscriber.events
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "#handle passes through the return value" do
|
||||||
|
result = @reporter.handle do
|
||||||
|
2 + 2
|
||||||
|
end
|
||||||
|
assert_equal 4, result
|
||||||
|
end
|
||||||
|
|
||||||
|
test "#handle returns nil on handled raise" do
|
||||||
|
result = @reporter.handle do
|
||||||
|
raise StandardError
|
||||||
|
2 + 2
|
||||||
|
end
|
||||||
|
assert_nil result
|
||||||
|
end
|
||||||
|
|
||||||
|
test "#handle returns a fallback value on handled raise" do
|
||||||
|
expected = "four"
|
||||||
|
result = @reporter.handle(fallback: expected) do
|
||||||
|
raise StandardError
|
||||||
|
2 + 2
|
||||||
|
end
|
||||||
|
assert_equal expected, result
|
||||||
|
end
|
||||||
|
|
||||||
test "#record report any unhandled error and re-raise them" do
|
test "#record report any unhandled error and re-raise them" do
|
||||||
error = ArgumentError.new("Oops")
|
error = ArgumentError.new("Oops")
|
||||||
assert_raises ArgumentError do
|
assert_raises ArgumentError do
|
||||||
|
@ -77,6 +101,13 @@ class ErrorReporterTest < ActiveSupport::TestCase
|
||||||
assert_equal [], @subscriber.events
|
assert_equal [], @subscriber.events
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "#record passes through the return value" do
|
||||||
|
result = @reporter.record do
|
||||||
|
2 + 2
|
||||||
|
end
|
||||||
|
assert_equal 4, result
|
||||||
|
end
|
||||||
|
|
||||||
test "can have multiple subscribers" do
|
test "can have multiple subscribers" do
|
||||||
second_subscriber = ErrorSubscriber.new
|
second_subscriber = ErrorSubscriber.new
|
||||||
@reporter.subscribe(second_subscriber)
|
@reporter.subscribe(second_subscriber)
|
||||||
|
|
Loading…
Reference in a new issue