From: deeshantk Date: Sat, 8 Nov 2025 15:52:45 +0000 (+0530) Subject: fix(conninfo): improve error message when host resolution fails X-Git-Tag: 3.3.0~21^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=aeb610641cbd13e55d9d562b2d8ef0d6e68e8612;p=thirdparty%2Fpsycopg.git fix(conninfo): improve error message when host resolution fails --- diff --git a/psycopg/psycopg/_conninfo_attempts.py b/psycopg/psycopg/_conninfo_attempts.py index f853e0537..b619ae78e 100644 --- a/psycopg/psycopg/_conninfo_attempts.py +++ b/psycopg/psycopg/_conninfo_attempts.py @@ -44,13 +44,15 @@ def conninfo_attempts(params: ConnMapping) -> list[ConnDict]: try: attempts.extend(_resolve_hostnames(attempt)) except OSError as ex: - logger.debug("failed to resolve host %r: %s", attempt.get("host"), ex) - last_exc = ex + last_exc = e.OperationalError( + f"failed to resolve host {attempt.get('host')!r}: {ex}" + ) + logger.debug("%s", last_exc) if not attempts: assert last_exc # We couldn't resolve anything - raise e.OperationalError(str(last_exc)) + raise last_exc if get_param(params, "load_balance_hosts") == "random": shuffle(attempts) diff --git a/psycopg/psycopg/_conninfo_attempts_async.py b/psycopg/psycopg/_conninfo_attempts_async.py index 08f363d1a..21228b4d4 100644 --- a/psycopg/psycopg/_conninfo_attempts_async.py +++ b/psycopg/psycopg/_conninfo_attempts_async.py @@ -42,13 +42,15 @@ async def conninfo_attempts_async(params: ConnMapping) -> list[ConnDict]: try: attempts.extend(await _resolve_hostnames(attempt)) except OSError as ex: - logger.debug("failed to resolve host %r: %s", attempt.get("host"), ex) - last_exc = ex + last_exc = e.OperationalError( + f"failed to resolve host {attempt.get('host')!r}: {ex}" + ) + logger.debug("%s", last_exc) if not attempts: assert last_exc # We couldn't resolve anything - raise e.OperationalError(str(last_exc)) + raise last_exc if get_param(params, "load_balance_hosts") == "random": shuffle(attempts)