instance method rescue_with_handler

Ruby on Rails 7.2.3

Since v5.2.8.1

Available in: v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

rescue_with_handler(exception, object: self, visited_exceptions: [])

Matches an exception to a handler based on the exception class.

If no handler matches the exception, check for a handler matching the (optional) exception.cause. If no handler matches the exception or its cause, this returns nil, so you can deal with unhandled exceptions. Be sure to re-raise unhandled exceptions if this is what you expect.

begin
  # ...
rescue => exception
  rescue_with_handler(exception) || raise
end

Returns the exception if it was handled and nil if it was not.

Parameters

exception req
object key = self
visited_exceptions key = []
Source
# File activesupport/lib/active_support/rescuable.rb, line 90
      def rescue_with_handler(exception, object: self, visited_exceptions: [])
        visited_exceptions << exception

        if handler = handler_for_rescue(exception, object: object)
          handler.call exception
          exception
        elsif exception
          if visited_exceptions.include?(exception.cause)
            nil
          else
            rescue_with_handler(exception.cause, object: object, visited_exceptions: visited_exceptions)
          end
        end
      end

Defined in activesupport/lib/active_support/rescuable.rb line 90 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveSupport::Rescuable::ClassMethods

Type at least 2 characters to search.

↑↓ navigate · open · esc close