From ffcd66561f377223ba59e63d134e8d6cc4b372ca Mon Sep 17 00:00:00 2001 From: Shane Harvey Date: Tue, 17 Apr 2018 16:55:44 -0700 Subject: [PATCH] PYTHON-1508 Add has_label to temporary transaction errors --- pymongo/errors.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pymongo/errors.py b/pymongo/errors.py index a94ae36a9..ac125645a 100644 --- a/pymongo/errors.py +++ b/pymongo/errors.py @@ -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