diff --git a/redis/asyncio/connection.py b/redis/asyncio/connection.py index 96f18876ea..b3dae2a27b 100644 --- a/redis/asyncio/connection.py +++ b/redis/asyncio/connection.py @@ -817,7 +817,7 @@ def _error_message(self, exception: BaseException) -> str: else: return ( f"Error {exception.args[0]} connecting to {host_error}. " - f"{exception.args[0]}." + f"{exception}." ) diff --git a/tests/test_asyncio/test_connection.py b/tests/test_asyncio/test_connection.py index 4ff3808602..6255ae7d6d 100644 --- a/tests/test_asyncio/test_connection.py +++ b/tests/test_asyncio/test_connection.py @@ -491,3 +491,21 @@ async def test_connection_garbage_collection(request): await client.aclose() await pool.aclose() + + +@pytest.mark.parametrize( + "error, expected_message", + [ + (OSError(), "Error connecting to localhost:6379. Connection reset by peer"), + (OSError(12), "Error connecting to localhost:6379. 12."), + ( + OSError(12, "Some Error"), + "Error 12 connecting to localhost:6379. [Errno 12] Some Error.", + ), + ], +) +async def test_connect_error_message(error, expected_message): + """Test that the _error_message function formats errors correctly""" + conn = Connection() + error_message = conn._error_message(error) + assert error_message == expected_message