module ActiveSupport::Rescuable::ClassMethods

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

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

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

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

Matches an exception to a handler based on the exception class.
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