instance method
handle
Ruby on Rails 7.2.3
Since v7.0.10Signature
handle(*error_classes, severity: :warning, context: {}, fallback: nil, source: DEFAULT_SOURCE)
Evaluates the given block, reporting and swallowing any unhandled error. If no error is raised, returns the return value of the block. Otherwise, returns the result of fallback.call, or nil if fallback is not specified.
# Will report a TypeError to all subscribers and return nil. Rails.error.handle do 1 + '1' end
Can be restricted to handle only specific error classes:
maybe_tags = Rails.error.handle(Redis::BaseError) { redis.get("tags") }
Options
-
:severity- This value is passed along to subscribers to indicate how important the error report is. Can be:error,:warning, or:info. Defaults to:warning. -
:context- Extra information that is passed along to subscribers. For example:Rails.error.handle(context: { section: "admin" }) do # ... end
-
:fallback- A callable that provideshandle‘s return value when an unhandled error is raised. For example:user = Rails.error.handle(fallback: -> { User.anonymous }) do User.find_by(params) end
-
:source- This value is passed along to subscribers to indicate the source of the error. Subscribers can use this value to ignore certain errors. Defaults to"application".
Parameters
-
error_classesrest -
severitykey = :warning -
contextkey = {} -
fallbackkey = nil -
sourcekey = DEFAULT_SOURCE
Source
# File activesupport/lib/active_support/error_reporter.rb, line 78
def handle(*error_classes, severity: :warning, context: {}, fallback: nil, source: DEFAULT_SOURCE)
error_classes = DEFAULT_RESCUE if error_classes.empty?
yield
rescue *error_classes => error
report(error, handled: true, severity: severity, context: context, source: source)
fallback.call if fallback
end
Defined in activesupport/lib/active_support/error_reporter.rb line 78
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveSupport::ErrorReporter