Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions adafruit_connection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def connect(self, address: Tuple[str, int]) -> None:
try:
return self._socket.connect(address, self._mode)
except RuntimeError as error:
raise OSError(errno.ENOMEM) from error
raise OSError(errno.ENOMEM, str(error)) from error
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

for ESP32SPI, a little more information



class _FakeSSLContext:
Expand Down Expand Up @@ -286,18 +286,23 @@ def get_socket(
host, port, 0, self._socket_pool.SOCK_STREAM
)[0]

first_exception = None
result = self._get_connected_socket(
addr_info, host, port, timeout, is_ssl, ssl_context
)
if isinstance(result, Exception):
# Got an error, if there are any available sockets, free them and try again
if self.available_socket_count:
first_exception = result
self._free_sockets()
result = self._get_connected_socket(
addr_info, host, port, timeout, is_ssl, ssl_context
)
if isinstance(result, Exception):
raise RuntimeError(f"Error connecting socket: {result}") from result
last_result = f", first error: {first_exception}" if first_exception else ""
raise RuntimeError(
f"Error connecting socket: {result}{last_result}"
) from result

self._key_by_managed_socket[result] = key
self._managed_socket_by_key[key] = result
Expand Down
6 changes: 3 additions & 3 deletions tests/get_socket_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def test_get_socket_runtime_error_ties_again_only_once():
# try to get a socket that returns a RuntimeError twice
with pytest.raises(RuntimeError) as context:
connection_manager.get_socket(mocket.MOCK_HOST_2, 80, "http:")
assert "Error connecting socket: error 2" in str(context)
assert "Error connecting socket: error 2, first error: error 1" in str(context)
free_sockets_mock.assert_called_once()


Expand Down Expand Up @@ -242,7 +242,7 @@ def test_fake_ssl_context_connect_error( # pylint: disable=unused-argument
mock_pool = mocket.MocketPool()
mock_socket_1 = mocket.Mocket()
mock_pool.socket.return_value = mock_socket_1
mock_socket_1.connect.side_effect = RuntimeError("RuntimeError ")
mock_socket_1.connect.side_effect = RuntimeError("RuntimeError")

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