Skip to content

Commit 4364b7c

Browse files
authored
PYTHON-2596 Include host in error message when connection is closed (mongodb#575)
Use raise from syntax when re-raising exceptions in the pool.
1 parent 4088c1c commit 4364b7c

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

pymongo/network.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
from pymongo import helpers, message
2727
from pymongo.common import MAX_MESSAGE_SIZE
2828
from pymongo.compression_support import decompress, _NO_COMPRESSION
29-
from pymongo.errors import (AutoReconnect,
30-
NotMasterError,
29+
from pymongo.errors import (NotMasterError,
3130
OperationFailure,
3231
ProtocolError,
3332
_OperationCancelled)
@@ -261,7 +260,7 @@ def _receive_data_on_socket(sock_info, length, deadline):
261260
continue
262261
raise
263262
if chunk_length == 0:
264-
raise AutoReconnect("connection closed")
263+
raise OSError("connection closed")
265264

266265
bytes_read += chunk_length
267266

pymongo/pool.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,16 +236,9 @@ def _raise_connection_failure(address, error, msg_prefix=None):
236236
if msg_prefix:
237237
msg = msg_prefix + msg
238238
if isinstance(error, socket.timeout):
239-
raise NetworkTimeout(msg)
240-
elif isinstance(error, _SSLError) and 'timed out' in str(error):
241-
# CPython 2.7 and PyPy 2.x do not distinguish network
242-
# timeouts from other SSLErrors (https://bugs.python.org/issue10272).
243-
# Luckily, we can work around this limitation because the phrase
244-
# 'timed out' appears in all the timeout related SSLErrors raised
245-
# on the above platforms.
246-
raise NetworkTimeout(msg)
239+
raise NetworkTimeout(msg) from error
247240
else:
248-
raise AutoReconnect(msg)
241+
raise AutoReconnect(msg) from error
249242

250243

251244
def _cond_wait(condition, deadline):

test/test_client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,18 @@ def server_description_count():
15681568
# AssertionError: 4 != 22 within 5 delta (18 difference)
15691569
self.assertAlmostEqual(initial_count, final_count, delta=10)
15701570

1571+
@client_context.require_failCommand_fail_point
1572+
def test_network_error_message(self):
1573+
client = single_client(retryReads=False)
1574+
self.addCleanup(client.close)
1575+
client.admin.command('ping') # connect
1576+
with self.fail_point({'mode': {'times': 1},
1577+
'data': {'closeConnection': True,
1578+
'failCommands': ['find']}}):
1579+
expected = '%s:%s: connection closed' % client.address
1580+
with self.assertRaisesRegex(AutoReconnect, expected):
1581+
client.pymongo_test.test.find_one({})
1582+
15711583

15721584
class TestExhaustCursor(IntegrationTest):
15731585
"""Test that clients properly handle errors from exhaust cursors."""

0 commit comments

Comments
 (0)