Skip to content

Add request context to HTTPClientError.unexpectedStatusCode and CustomStringConvertible conformance - #175

Open
krishnapermi wants to merge 2 commits into
apple:mainfrom
krishnapermi:improve-auth-error-messages
Open

Add request context to HTTPClientError.unexpectedStatusCode and CustomStringConvertible conformance#175
krishnapermi wants to merge 2 commits into
apple:mainfrom
krishnapermi:improve-auth-error-messages

Conversation

@krishnapermi

Copy link
Copy Markdown

Closes #118

What

Two related changes to HTTPClientError:

  1. Adds the originating HTTPRequest to the unexpectedStatusCode case. The other two error cases that involve a remote server (unauthorized, authenticationChallenge) already carry the request so callers can surface the registry hostname in diagnostics. unexpectedStatusCode was the odd one out.

  2. Adds CustomStringConvertible conformance. The implementation uses the new request field to produce user-readable messages that include the registry hostname and a tailored explanation for 401/403 responses, for example:

    Authentication failed for registry "ghcr.io": the server returned HTTP 401. Ensure your credentials are correct.
    Registry "ghcr.io" returned an unexpected HTTP 404 response.
    

I noticed the inconsistency while reading through the error type — unauthorized and authenticationChallenge both thread the request through so their error messages can mention the host, but unexpectedStatusCode dropped it on the floor. This meant the most common error case (any non-2xx response) produced the least informative message.

Changes

  • Sources/ContainerRegistry/HTTPClient.swift: adds request: HTTPRequest as the first label on unexpectedStatusCode, threads the request through both throw sites in validateAPIResponseThrowing, and adds the CustomStringConvertible extension.
  • Sources/ContainerRegistry/RegistryClient.swift: updates the two catch clauses that pattern-match on unexpectedStatusCode to bind a wildcard for the new leading request parameter.

…le conformance

Adds the originating HTTPRequest to the unexpectedStatusCode error case so callers can surface the registry hostname in diagnostics. Also adds CustomStringConvertible to produce user-friendly messages like "Authentication failed for registry \"ghcr.io\": the server returned HTTP 401."
…dition

The unexpectedStatusCode enum case now carries a leading request parameter. Update the two catch patterns in RegistryClient to bind a wildcard for it so the code continues to compile.
@euanh
euanh force-pushed the improve-auth-error-messages branch from 2d4725e to 30471a1 Compare September 7, 2026 08:34
}
}

extension HTTPClientError: CustomStringConvertible {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This duplicates the CustomStringConvertible extension defined in containertool, which causes the compiler to print a warning:

extension HTTPClientError: Swift.CustomStringConvertible {

I think it's better to let the client define the custom printing of error messages, instead than doing it the library, because the client may want to present the errors in a particular way.

@euanh euanh added kind/enhancement New feature or request semver/patch No public API change. labels Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement New feature or request semver/patch No public API change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

containertool should print better error messages for authentication failures

2 participants