Я немного поработал с python-couchdb и desktopcouch. В одном из представленных мной патчей я обернул функцию db.update
из couchdb. Для тех, кто не знаком с python-couchdb, эта функция выглядит следующим образом:
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
"""
Как видите, эта функция не вызывает исключения, которые были вызваны сервером couchdb, а скорее возвращает их в кортеже с идентификатором документа, который мы хотели обновить.
Один из рецензентов отправился на #python по irc, чтобы спросить об этом. В #python они рекомендовали использовать дозорные значения, а не исключения. Как вы видите, просто подход не практичен, так как есть много возможных исключений, которые могут быть получены. Мои вопросы: каковы преимущества использования исключений над значениями часового, кроме того, что использование исключений более уродливо?