Exception Types and Classifiers¶
New in version 1.2.1.
Couchbase exceptions may be caught in two flavors. You can catch an exception either by its explicit subclass or by its base class.
Normally you should catch exception classes for cases you don’t have a specific handler for, and error details for things that need special handling. Thus for example:
try: cb.get("foo") except DocumentNotFoundException: print("Item does not exist") except CouchbaseTransientException: print("Transient error received. Handling and backing off")
Where DocumentNotFoundException is a specific error detail code indicating the item has not been found, and CouchbaseTransientException is an error category indicating the specific cause is likely transient.
As your application evolves you may wish to examine the specific error code received as well as log the information to the screen.
Employing the error classifier pattern will prove scalable when additional error codes are added to the library. Sticking to catching error codes rather than specific error categories will allow your application to deal gracefully with future error codes so long as they match a specific category.
You may also employ a different use model, for example:
try: cb.get("foo") except CouchbaseException as e: if e.is_data and isinstance(e, DocumentNotFoundException): # handle not found pass elif e.is_network: print("Got network error") elif e.is_data: print("Got other data-related error") else: print("Got unhandled error code")
Base Exception Object¶
This object is the base class for all exceptions thrown by Couchbase which are specific to the library. Other standard Python exceptions may still be thrown depending on the condition.
These categories form the base exception classes
The following codes are exception details. They all derive from
CouchbaseException. Many of them will have multiple error categories and thus
be inherited from multiple exception categories.