Skip to content

client: improve handling of JSON error-responses with incorrect schema#49373

Merged
thaJeztah merged 1 commit intomoby:masterfrom
thaJeztah:client_improve_error_response_handling
Jan 31, 2025
Merged

client: improve handling of JSON error-responses with incorrect schema#49373
thaJeztah merged 1 commit intomoby:masterfrom
thaJeztah:client_improve_error_response_handling

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

follow-up to:

Before this patch, an API response that's valid JSON, but not the right schema would be silently discarded by the CLI. For example, due to a bug in Docker Desktop's API proxy, the "normal" (not JSON error) response would be returned together with a non-200 status code when using an unsupported API version;

curl -s -w 'STATUS: %{http_code}\n' --unix-socket /var/run/docker.sock 'http://localhost/v1.99/version'
{"Platform":{"Name":"Docker Desktop 4.38.0 (181016)"},"Version":"","ApiVersion":"","GitCommit":"","GoVersion":"","Os":"","Arch":""}
STATUS: 400

Before this patch, this resulted in no output being shown;

DOCKER_API_VERSION=1.99 docker version
Client:
 Version:           27.5.1
 API version:       1.99 (downgraded from 1.47)
 Go version:        go1.22.11
 Git commit:        9f9e405
 Built:             Wed Jan 22 13:37:19 2025
 OS/Arch:           darwin/arm64
 Context:           desktop-linux
Error response from daemon:

With this patch, an error is generated based on the status:

DOCKER_API_VERSION=1.99 docker version
Client:
 Version:           27.5.1
 API version:       1.99 (downgraded from 1.47)
 Go version:        go1.22.11
 Git commit:        9f9e405
 Built:             Wed Jan 22 13:37:19 2025
 OS/Arch:           darwin/arm64
 Context:           desktop-linux
Error response from daemon: API returned a 400 (Bad Request) but provided no error-message

- What I did

- How I did it

- How to verify it

- Description for the changelog

Improve handling of invalid API errors that could result in an empty error message being presented to the user.

- A picture of a cute animal (not mandatory but encouraged)

Before this patch, an API response that's valid JSON, but not the right
schema would be silently discarded by the CLI. For example, due to a bug
in Docker Desktop's API proxy, the "normal" (not JSON error) response
would be returned together with a non-200 status code when using an
unsupported API version;

    curl -s -w 'STATUS: %{http_code}\n' --unix-socket /var/run/docker.sock 'http://localhost/v1.99/version'
    {"Platform":{"Name":"Docker Desktop 4.38.0 (181016)"},"Version":"","ApiVersion":"","GitCommit":"","GoVersion":"","Os":"","Arch":""}
    STATUS: 400

Before this patch, this resulted in no output being shown;

    DOCKER_API_VERSION=1.99 docker version
    Client:
     Version:           27.5.1
     API version:       1.99 (downgraded from 1.47)
     Go version:        go1.22.11
     Git commit:        9f9e405
     Built:             Wed Jan 22 13:37:19 2025
     OS/Arch:           darwin/arm64
     Context:           desktop-linux
    Error response from daemon:

With this patch, an error is generated based on the status:

    DOCKER_API_VERSION=1.99 docker version
    Client:
     Version:           27.5.1
     API version:       1.99 (downgraded from 1.47)
     Go version:        go1.22.11
     Git commit:        9f9e405
     Built:             Wed Jan 22 13:37:19 2025
     OS/Arch:           darwin/arm64
     Context:           desktop-linux
    Error response from daemon: API returned a 400 (Bad Request) but provided no error-message

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah thaJeztah added area/api API status/2-code-review kind/enhancement Enhancements are not bugs or new features but can improve usability or performance. impact/cli labels Jan 30, 2025
@thaJeztah thaJeztah added this to the 28.0.0 milestone Jan 30, 2025
@thaJeztah thaJeztah self-assigned this Jan 30, 2025
Comment thread client/request.go
@@ -234,8 +234,35 @@ func (cli *Client) checkResponseErr(serverResp serverResponse) error {
if err := json.Unmarshal(body, &errorResponse); err != nil {
return errors.Wrap(err, "Error reading JSON")
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Probably one more follow-up coming for this case, where we could end up with a proxy returning HTML with an invalid content-type header, which may result in;

Error reading JSON: invalid character '<' looking for beginning of value

Possibly we could consider;

  • by default capture something similar to below (include the failure to parse, but also reply with status-code and "text")
  • adding a debug log to log the response, so that a user could enable --debug to see the problem
  • maybe same for text/html responses, which are unexpected, but we may not always want to dump the HTML as-is.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

☝️ I had some WIP changes for both, but not yet complete, so I need to dust them off a bit; thought I'd push this PR already, as this one is already OK to review

@thaJeztah thaJeztah requested review from robmry and vvoland January 30, 2025 21:10
@thaJeztah
Copy link
Copy Markdown
Member Author

Thanks! I'll bring this one in; I'll have a look at those follow-ups mentioned, but I think this one at least is a small improvement on its own.

@thaJeztah thaJeztah merged commit f88304a into moby:master Jan 31, 2025
@thaJeztah thaJeztah deleted the client_improve_error_response_handling branch January 31, 2025 12:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/api API impact/cli kind/enhancement Enhancements are not bugs or new features but can improve usability or performance. status/2-code-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants