From 67a23429bae8fdfa2db5d768a34060c132984886 Mon Sep 17 00:00:00 2001 From: Julius Park Date: Tue, 30 Jun 2020 12:30:28 -0400 Subject: [PATCH] PYTHON-1787: add details to OperationFailure exception and NotMasterError (#448) PYTHON-1787-add details to OperationFailure and NotMasterError by adding a __repr__ function https://jira.mongodb.org/browse/PYTHON-1787 --- doc/contributors.rst | 1 + pymongo/errors.py | 11 ++++++++++- test/test_database.py | 6 +++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/doc/contributors.rst b/doc/contributors.rst index 9773b3822..4118d5558 100644 --- a/doc/contributors.rst +++ b/doc/contributors.rst @@ -87,3 +87,4 @@ The following is a list of people who have contributed to - Felipe Rodrigues(fbidu) - Terence Honles (terencehonles) - Paul Fisher (thetorpedodog) +- Julius Park (juliusgeo) diff --git a/pymongo/errors.py b/pymongo/errors.py index c11053f47..cf3254685 100644 --- a/pymongo/errors.py +++ b/pymongo/errors.py @@ -113,7 +113,11 @@ class NotMasterError(AutoReconnect): Subclass of :exc:`~pymongo.errors.AutoReconnect`. """ - + def __str__(self): + output_str = "%s, full error: %s" % (self._message, self.__details) + if sys.version_info[0] == 2 and isinstance(output_str, unicode): + return output_str.encode('utf-8', errors='replace') + return output_str class ServerSelectionTimeoutError(AutoReconnect): """Thrown when no MongoDB server is available for an operation @@ -167,6 +171,11 @@ class OperationFailure(PyMongoError): """ return self.__details + def __str__(self): + output_str = "%s, full error: %s" % (self._message, self.__details) + if sys.version_info[0] == 2 and isinstance(output_str, unicode): + return output_str.encode('utf-8', errors='replace') + return output_str class CursorNotFound(OperationFailure): """Raised while iterating query results if the cursor is diff --git a/test/test_database.py b/test/test_database.py index 15f3be70f..43fac30b0 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -972,7 +972,7 @@ class TestDatabase(IntegrationTest): with self.assertRaises(OperationFailure) as context: helpers._check_command_response(error_document) - self.assertEqual('inner', str(context.exception)) + self.assertIn('inner', str(context.exception)) # If a shard has no primary and you run a command like dbstats, which # cannot be run on a secondary, mongos's response includes empty "raw" @@ -985,7 +985,7 @@ class TestDatabase(IntegrationTest): with self.assertRaises(OperationFailure) as context: helpers._check_command_response(error_document) - self.assertEqual('outer', str(context.exception)) + self.assertIn('outer', str(context.exception)) # Raw error has ok: 0 but no errmsg. Not a known case, but test it. error_document = { @@ -996,7 +996,7 @@ class TestDatabase(IntegrationTest): with self.assertRaises(OperationFailure) as context: helpers._check_command_response(error_document) - self.assertEqual('outer', str(context.exception)) + self.assertIn('outer', str(context.exception)) @client_context.require_test_commands @client_context.require_no_mongos