What are the disadvantages of returning an Exception instance instead of raising it in Python?

I work with python-couchdb and desktopcouch. In one of the patches that I submitted, I wrapped a function db.updatefrom couchdb. For anyone new to python-couchdb, the function is as follows:

def update(self, documents, **options):
    """Perform a bulk update or insertion of the given documents using a
    single HTTP request.

    >>> server = Server('http://localhost:5984/')
    >>> db = server.create('python-tests')
    >>> for doc in db.update([
    ...     Document(type='Person', name='John Doe'),
    ...     Document(type='Person', name='Mary Jane'),
    ...     Document(type='City', name='Gotham City')
    ... ]):
    ...     print repr(doc) #doctest: +ELLIPSIS
    (True, '...', '...')
    (True, '...', '...')
    (True, '...', '...')

    >>> del server['python-tests']

    The return value of this method is a list containing a tuple for every
    element in the `documents` sequence. Each tuple is of the form
    ``(success, docid, rev_or_exc)``, where ``success`` is a boolean
    indicating whether the update succeeded, ``docid`` is the ID of the
    document, and ``rev_or_exc`` is either the new document revision, or
    an exception instance (e.g. `ResourceConflict`) if the update failed.

    If an object in the documents list is not a dictionary, this method
    looks for an ``items()`` method that can be used to convert the object
    to a dictionary. Effectively this means you can also use this method
    with `schema.Document` objects.

    :param documents: a sequence of dictionaries or `Document` objects, or
                      objects providing a ``items()`` method that can be
                      used to convert them to a dictionary
    :return: an iterable over the resulting documents
    :rtype: ``list``

    :since: version 0.2
    """

As you can see, this function does not raise exceptions that were raised by the couchdb server, but rather returns them to the tuple with the identifier of the document we wanted to update.

#python on irc, . #python , . , , , . : , , ?

+3
4

, , , . , API , .

+4

- , -, , . , , , , , .

, . , , .

Python . , foo < bar, foo.__lt__(bar). , , . NotImplemented, Python bar.__ge__(foo). , , , , .

, IMO.

+2

, . , .

, , , . ? - , ? .

, docid, new_rev_doc tuple / , . success .

+1

break;, , , , . , , . , , .

, (, C), , , ; .

, update() :

  • ; , , .
  • , . .

API. , db , - ( ).

By the way, if your business logic requires that all operations complete successfully, and you don’t know what to do when the update fails (i.e. your design says that this should never happen), feel free to throw an exception in your own code.

+1
source

Source: https://habr.com/ru/post/1729552/


All Articles