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
This commit is contained in:
Julius Park 2020-06-30 12:30:28 -04:00 committed by GitHub
parent 58aaede0fe
commit 67a23429ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 4 deletions

View File

@ -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)

View File

@ -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

View File

@ -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