Add address hint to connect errors when PostgreSQL is unreachable - #1344
Open
aryansk wants to merge 1 commit into
Open
Add address hint to connect errors when PostgreSQL is unreachable#1344aryansk wants to merge 1 commit into
aryansk wants to merge 1 commit into
Conversation
ConnectionRefusedError is identical whether the server is not running or is simply listening on a different port. Include the attempted address in the re-raised error so users can spot port mismatches immediately instead of digging for the cause. Fixes MagicStack#1342 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes #1342 — when a connection attempt fails, the raised error now names the address that was tried, so a PostgreSQL server listening on a different port is distinguishable from a server that is not running.
Why
ConnectionRefusedErroris byte-for-byte identical whether the server is down or simply listening elsewhere. The classic failure mode isconnect(port=5433)against a server on 5432, or a wrong hostname — the error message gives no clue which address was actually attempted, and users assume PostgreSQL is broken instead of checking the port.How
_connect()keeps the last attemptedaddralongside the last error and, when re-raising, appends a hint:The original exception is preserved as
__cause__and the same exception type is re-raised, so existingexcept OSError/except ConnectionRefusedErrorhandling is unaffected. TheTargetServerAttributeNotMatchedbranch (noOSErrorat all) is unchanged. Unix socket paths are formatted aspath/.s.PGSQL.port, matching asyncpg's convention.Tests
Added
TestConnectErrorHintintests/test_connect.py:test_connect_error_includes_address_hint— a refused TCP connection reports127.0.0.1:5432in the message, and__cause__is the original error (fails before the fix: message lacks the address).test_connect_error_hint_for_unix_socket— a missing socket reports/var/run/postgresql/.s.PGSQL.5432(fails before: message lacks the socket).Both use
unittest.mock.patch.object(connect_utils, '_connect_addr', ...)so they run without a live server. Fulltests/test_connect.pysuite passes.🤖 Generated with Codebuff