Skip to content

Commit 601ee66

Browse files
committed
Little error cleanup
1 parent f33d78c commit 601ee66

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

adafruit_connection_manager.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def connect(self, address: Tuple[str, int]) -> None:
6464
try:
6565
return self._socket.connect(address, self._mode)
6666
except RuntimeError as error:
67-
raise OSError(errno.ENOMEM) from error
67+
raise OSError(errno.ENOMEM, str(error)) from error
6868

6969

7070
class _FakeSSLContext:
@@ -286,18 +286,23 @@ def get_socket(
286286
host, port, 0, self._socket_pool.SOCK_STREAM
287287
)[0]
288288

289+
first_exception = None
289290
result = self._get_connected_socket(
290291
addr_info, host, port, timeout, is_ssl, ssl_context
291292
)
292293
if isinstance(result, Exception):
293294
# Got an error, if there are any available sockets, free them and try again
294295
if self.available_socket_count:
296+
first_exception = result
295297
self._free_sockets()
296298
result = self._get_connected_socket(
297299
addr_info, host, port, timeout, is_ssl, ssl_context
298300
)
299301
if isinstance(result, Exception):
300-
raise RuntimeError(f"Error connecting socket: {result}") from result
302+
last_result = f", first error: {first_exception}" if first_exception else ""
303+
raise RuntimeError(
304+
f"Error connecting socket: {result}{last_result}"
305+
) from result
301306

302307
self._key_by_managed_socket[result] = key
303308
self._managed_socket_by_key[key] = result

tests/get_socket_test.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def test_get_socket_runtime_error_ties_again_only_once():
213213
# try to get a socket that returns a RuntimeError twice
214214
with pytest.raises(RuntimeError) as context:
215215
connection_manager.get_socket(mocket.MOCK_HOST_2, 80, "http:")
216-
assert "Error connecting socket: error 2" in str(context)
216+
assert "Error connecting socket: error 2, first error: error 1" in str(context)
217217
free_sockets_mock.assert_called_once()
218218

219219

@@ -242,7 +242,7 @@ def test_fake_ssl_context_connect_error( # pylint: disable=unused-argument
242242
mock_pool = mocket.MocketPool()
243243
mock_socket_1 = mocket.Mocket()
244244
mock_pool.socket.return_value = mock_socket_1
245-
mock_socket_1.connect.side_effect = RuntimeError("RuntimeError ")
245+
mock_socket_1.connect.side_effect = RuntimeError("RuntimeError")
246246

247247
radio = mocket.MockRadio.ESP_SPIcontrol()
248248
ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio)
@@ -252,4 +252,4 @@ def test_fake_ssl_context_connect_error( # pylint: disable=unused-argument
252252
connection_manager.get_socket(
253253
mocket.MOCK_HOST_1, 443, "https:", ssl_context=ssl_context
254254
)
255-
assert "Error connecting socket: 12" in str(context)
255+
assert "Error connecting socket: [Errno 12] RuntimeError" in str(context)

0 commit comments

Comments
 (0)