From 8b2d2952b7b6ca1c5364e00fa633b97ef648fd22 Mon Sep 17 00:00:00 2001 From: "dmitry.kanev" Date: Sat, 20 Apr 2024 14:36:28 +0400 Subject: [PATCH] Add details to the asyncio connection error message --- redis/asyncio/connection.py | 2 +- tests/test_asyncio/test_connection.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/redis/asyncio/connection.py b/redis/asyncio/connection.py index 2e470bfcfb..8d6f8e807f 100644 --- a/redis/asyncio/connection.py +++ b/redis/asyncio/connection.py @@ -805,7 +805,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