client: improve handling of JSON error-responses with incorrect schema#49373
Merged
thaJeztah merged 1 commit intomoby:masterfrom Jan 31, 2025
Merged
Conversation
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
commented
Jan 30, 2025
| @@ -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") | |||
Member
Author
There was a problem hiding this comment.
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
--debugto see the problem - maybe same for
text/htmlresponses, which are unexpected, but we may not always want to dump the HTML as-is.
Member
Author
There was a problem hiding this comment.
☝️ 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
vvoland
approved these changes
Jan 31, 2025
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. |
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.
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;
Before this patch, this resulted in no output being shown;
With this patch, an error is generated based on the status:
- What I did
- How I did it
- How to verify it
- Description for the changelog
- A picture of a cute animal (not mandatory but encouraged)