Skip to content

Preserve getaddrinfo error code in SocketAddressError#3558

Open
thisismanan wants to merge 1 commit intoapple:mainfrom
thisismanan:getaddrinfo-err-code
Open

Preserve getaddrinfo error code in SocketAddressError#3558
thisismanan wants to merge 1 commit intoapple:mainfrom
thisismanan:getaddrinfo-err-code

Conversation

@thisismanan
Copy link
Copy Markdown

@thisismanan thisismanan commented Mar 24, 2026

As observed by @weissi #3382

Motivation:

When getaddrinfo fails, SwiftNIO throws SocketAddressError.unknown which discards the error code. This makes it impossible for callers to
distinguish between different failure reasons (e.g., EAI_NONAME vs EAI_AGAIN), which is needed for retry logic and diagnostics.

Modifications:

  • Add SocketAddressError.UnknownHost struct that carries host, port, errorCode (from getaddrinfo), and errorDescription (from
    gai_strerror).
  • Update SocketAddress.makeAddressResolvingHost and GetaddrinfoResolver to throw/fail with the new error type on both POSIX and Windows paths.
  • Update existing test catch blocks to use the new error type.
  • Add testGetaddrinfoErrorCodeIsPreserved to verify the error code is captured.

Result:

Callers can now inspect the getaddrinfo error code when host resolution fails, enabling more specific error handling. Resolves #3382

Questions for reviewer

  1. Should case unknown(host:port:) be deprecated? — This enum case is no longer thrown after this PR. Callers catching .unknown will silently stop matching. Should I add
    @available(*, deprecated, renamed: "UnknownHost") to provide a migration path?

  2. Should the doc comment on makeAddressResolvingHost be updated? — The - Throws: line still references SocketAddressError.unknown instead of the new
    SocketAddressError.UnknownHost.

Motivation:

 When getaddrinfo fails, SwiftNIO throws SocketAddressError.unknown which
 discards the error code. This makes it impossible for callers to distinguish
 between different failure reasons (e.g., EAI_NONAME vs EAI_AGAIN), which is
 needed for retry logic and diagnostics.

Modifications:
 - Add SocketAddressError.UnknownHost struct that carries host, port, errorCode
   (from getaddrinfo), and errorDescription (from gai_strerror).
 - Update SocketAddress.makeAddressResolvingHost and GetaddrinfoResolver to
   throw/fail with the new error type on both POSIX and Windows paths.
 - Update existing test catch blocks to use the new error type.
 - Add testGetaddrinfoErrorCodeIsPreserved to verify the error code is captured

Result:

 Callers can now inspect the getaddrinfo error code when host resolution fails,
 enabling more specific error handling.
@thisismanan
Copy link
Copy Markdown
Author

Hey @Lukasa, just checking if you've had a chance to look at this. Happy to make any changes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getaddrinfo resolver throws away error code

1 participant