Skip to content

PYTHON-1787: fix NotMasterError wrong attribute error #450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 2, 2020
2 changes: 1 addition & 1 deletion pymongo/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class NotMasterError(AutoReconnect):
Subclass of :exc:`~pymongo.errors.AutoReconnect`.
"""
def __str__(self):
Copy link
Member

@ShaneHarvey ShaneHarvey Jun 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed earlier today, can you add a test that asserts that the "full error" string is included in str(exc) and traceback.format_exc()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

output_str = "%s, full error: %s" % (self._message, self.__details)
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
Expand Down
3 changes: 3 additions & 0 deletions test/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@ def test_not_master_error(self):
client.pymongo_test.test.find_one_and_delete({})
except NotMasterError as exc:
error = exc.errors
import traceback
self.assertIn("full error", str(exc))
self.assertIn("full error", traceback.format_exc())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly what I had in mind. What do you think about moving these assertions to their own test cases (maybe to a new file test_errors.py)? You can use the error classes directly, like:

exc = NotMasterError(...)
self.assertIn("full error", str(exc))
try:
    raise exc
except NotMasterError:
    self.assertIn("full error", traceback.format_exc())

results = self.listener.results
started = results['started'][0]
failed = results['failed'][0]
Expand Down