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.
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.

containertool should print better error messages for authentication failures

1 participant