instance method
unexpected
Ruby on Rails 8.0.4
Since v7.2.3Signature
unexpected(error, severity: :warning, context: {}, source: DEFAULT_SOURCE)
Either report the given error when in production, or raise it when in development or test.
When called in production, after the error is reported, this method will return nil and execution will continue.
When called in development, the original error is wrapped in a different error class to ensure it’s not being rescued higher in the stack and will be surfaced to the developer.
This method is intended for reporting violated assertions about preconditions, or similar cases that can and should be gracefully handled in production, but that aren’t supposed to happen.
The error can be either an exception instance or a String.
example:
def edit
if published?
Rails.error.unexpected("[BUG] Attempting to edit a published article, that shouldn't be possible")
return false
end
# ...
end
Parameters
-
errorreq -
severitykey = :warning -
contextkey = {} -
sourcekey = DEFAULT_SOURCE
Source
# File activesupport/lib/active_support/error_reporter.rb, line 145
def unexpected(error, severity: :warning, context: {}, source: DEFAULT_SOURCE)
error = RuntimeError.new(error) if error.is_a?(String)
if @debug_mode
ensure_backtrace(error)
raise UnexpectedError, "#{error.class.name}: #{error.message}", error.backtrace, cause: error
else
report(error, handled: true, severity: severity, context: context, source: source)
end
end
Defined in activesupport/lib/active_support/error_reporter.rb line 145
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveSupport::ErrorReporter