Skip to content

Commit 67a2342

Browse files
authored
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
1 parent 58aaede commit 67a2342

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

doc/contributors.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,4 @@ The following is a list of people who have contributed to
8787
- Felipe Rodrigues(fbidu)
8888
- Terence Honles (terencehonles)
8989
- Paul Fisher (thetorpedodog)
90+
- Julius Park (juliusgeo)

pymongo/errors.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ class NotMasterError(AutoReconnect):
113113
114114
Subclass of :exc:`~pymongo.errors.AutoReconnect`.
115115
"""
116-
116+
def __str__(self):
117+
output_str = "%s, full error: %s" % (self._message, self.__details)
118+
if sys.version_info[0] == 2 and isinstance(output_str, unicode):
119+
return output_str.encode('utf-8', errors='replace')
120+
return output_str
117121

118122
class ServerSelectionTimeoutError(AutoReconnect):
119123
"""Thrown when no MongoDB server is available for an operation
@@ -167,6 +171,11 @@ def details(self):
167171
"""
168172
return self.__details
169173

174+
def __str__(self):
175+
output_str = "%s, full error: %s" % (self._message, self.__details)
176+
if sys.version_info[0] == 2 and isinstance(output_str, unicode):
177+
return output_str.encode('utf-8', errors='replace')
178+
return output_str
170179

171180
class CursorNotFound(OperationFailure):
172181
"""Raised while iterating query results if the cursor is

test/test_database.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ def test_mongos_response(self):
972972
with self.assertRaises(OperationFailure) as context:
973973
helpers._check_command_response(error_document)
974974

975-
self.assertEqual('inner', str(context.exception))
975+
self.assertIn('inner', str(context.exception))
976976

977977
# If a shard has no primary and you run a command like dbstats, which
978978
# cannot be run on a secondary, mongos's response includes empty "raw"
@@ -985,7 +985,7 @@ def test_mongos_response(self):
985985
with self.assertRaises(OperationFailure) as context:
986986
helpers._check_command_response(error_document)
987987

988-
self.assertEqual('outer', str(context.exception))
988+
self.assertIn('outer', str(context.exception))
989989

990990
# Raw error has ok: 0 but no errmsg. Not a known case, but test it.
991991
error_document = {
@@ -996,7 +996,7 @@ def test_mongos_response(self):
996996
with self.assertRaises(OperationFailure) as context:
997997
helpers._check_command_response(error_document)
998998

999-
self.assertEqual('outer', str(context.exception))
999+
self.assertIn('outer', str(context.exception))
10001000

10011001
@client_context.require_test_commands
10021002
@client_context.require_no_mongos

0 commit comments

Comments
 (0)