PYTHON-1508 Add has_label to temporary transaction errors

This commit is contained in:
Shane Harvey 2018-04-17 16:55:44 -07:00
parent d1ee646d72
commit ffcd66561f

View File

@ -33,6 +33,13 @@ class ProtocolError(PyMongoError):
class ConnectionFailure(PyMongoError):
"""Raised when a connection to the database cannot be made or is lost."""
def has_label(self, label):
"""Return True if this error contains the given label.
.. versionadded:: 3.7
"""
return label == "TemporaryTxnFailure"
class AutoReconnect(ConnectionFailure):
"""Raised when a connection to the database is lost and an attempt to
@ -124,6 +131,16 @@ class OperationFailure(PyMongoError):
"""
return self.__details
def has_label(self, label):
"""Return True if this error contains the given label.
.. versionadded:: 3.7
"""
if label == "TemporaryTxnFailure":
# WriteConflict, TransactionAborted, and NoSuchTransaction.
return self.__code in (112, 244, 251)
return False
class CursorNotFound(OperationFailure):
"""Raised while iterating query results if the cursor is