Skip to content

Commit a075eb7

Browse files
authored
PYTHON-1787: fix NotMasterError no attribute error (#450)
1 parent 4457714 commit a075eb7

File tree

4 files changed

+88
-12
lines changed

4 files changed

+88
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ pymongo.egg-info/
1414
*.egg
1515
.tox
1616
mongocryptd.pid
17+
.idea/

pymongo/errors.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ class NetworkTimeout(AutoReconnect):
100100
"""
101101

102102

103+
def _format_detailed_error(message, details):
104+
if details is not None:
105+
message = "%s, full error: %s" % (message, details)
106+
if sys.version_info[0] == 2 and isinstance(message, unicode):
107+
message = message.encode('utf-8', errors='replace')
108+
return message
109+
110+
103111
class NotMasterError(AutoReconnect):
104112
"""The server responded "not master" or "node is recovering".
105113
@@ -113,11 +121,10 @@ class NotMasterError(AutoReconnect):
113121
114122
Subclass of :exc:`~pymongo.errors.AutoReconnect`.
115123
"""
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
124+
def __init__(self, message='', errors=None):
125+
super(NotMasterError, self).__init__(
126+
_format_detailed_error(message, errors), errors=errors)
127+
121128

122129
class ServerSelectionTimeoutError(AutoReconnect):
123130
"""Thrown when no MongoDB server is available for an operation
@@ -149,7 +156,7 @@ def __init__(self, error, code=None, details=None, max_wire_version=None):
149156
if details is not None:
150157
error_labels = details.get('errorLabels')
151158
super(OperationFailure, self).__init__(
152-
error, error_labels=error_labels)
159+
_format_detailed_error(error, details), error_labels=error_labels)
153160
self.__code = code
154161
self.__details = details
155162
self.__max_wire_version = max_wire_version
@@ -176,11 +183,6 @@ def details(self):
176183
"""
177184
return self.__details
178185

179-
def __str__(self):
180-
output_str = "%s, full error: %s" % (self._message, self.__details)
181-
if sys.version_info[0] == 2 and isinstance(output_str, unicode):
182-
return output_str.encode('utf-8', errors='replace')
183-
return output_str
184186

185187

186188
class CursorNotFound(OperationFailure):

test/test_database.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ def test_command_response_without_ok(self):
959959
try:
960960
helpers._check_command_response({'$err': 'foo'}, None)
961961
except OperationFailure as e:
962-
self.assertEqual(e.args[0], 'foo')
962+
self.assertEqual(e.args[0], "foo, full error: {'$err': 'foo'}")
963963
else:
964964
self.fail("_check_command_response didn't raise OperationFailure")
965965

test/test_errors.py

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright 2020-present MongoDB, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import sys
16+
import traceback
17+
18+
sys.path[0:0] = [""]
19+
20+
from pymongo.errors import (NotMasterError,
21+
OperationFailure)
22+
from test import (PyMongoTestCase,
23+
unittest)
24+
25+
26+
class TestErrors(PyMongoTestCase):
27+
def test_not_master_error(self):
28+
exc = NotMasterError("not master test", {"errmsg": "error"})
29+
self.assertIn("full error", str(exc))
30+
try:
31+
raise exc
32+
except NotMasterError:
33+
self.assertIn("full error", traceback.format_exc())
34+
35+
def test_operation_failure(self):
36+
exc = OperationFailure("operation failure test", 10,
37+
{"errmsg": "error"})
38+
self.assertIn("full error", str(exc))
39+
try:
40+
raise exc
41+
except OperationFailure:
42+
self.assertIn("full error", traceback.format_exc())
43+
44+
def _test_unicode_strs(self, exc):
45+
if sys.version_info[0] == 2:
46+
self.assertEqual("unicode \xf0\x9f\x90\x8d, full error: {"
47+
"'errmsg': u'unicode \\U0001f40d'}", str(exc))
48+
elif 'PyPy' in sys.version:
49+
# PyPy displays unicode in repr differently.
50+
self.assertEqual("unicode \U0001f40d, full error: {"
51+
"'errmsg': 'unicode \\U0001f40d'}", str(exc))
52+
else:
53+
self.assertEqual("unicode \U0001f40d, full error: {"
54+
"'errmsg': 'unicode \U0001f40d'}", str(exc))
55+
try:
56+
raise exc
57+
except Exception:
58+
self.assertIn("full error", traceback.format_exc())
59+
60+
def test_unicode_strs_operation_failure(self):
61+
exc = OperationFailure(u'unicode \U0001f40d', 10,
62+
{"errmsg": u'unicode \U0001f40d'})
63+
self._test_unicode_strs(exc)
64+
65+
def test_unicode_strs_not_master_error(self):
66+
exc = NotMasterError(u'unicode \U0001f40d',
67+
{"errmsg": u'unicode \U0001f40d'})
68+
self._test_unicode_strs(exc)
69+
70+
71+
72+
if __name__ == "__main__":
73+
unittest.main()

0 commit comments

Comments
 (0)