Skip to content

Commit

Permalink
transport: preserve unexpected json errors (#739)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonjohnsonjr committed Jul 8, 2020
1 parent 72edbad commit 72597da
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pkg/v1/remote/transport/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,14 @@ func CheckError(resp *http.Response, codes ...int) error {

// https://github.com/docker/distribution/blob/master/docs/spec/api.md#errors
structuredError := &Error{}
if err := json.Unmarshal(b, structuredError); err != nil {
structuredError.rawBody = string(b)
}

// This can fail if e.g. the response body is not valid JSON. That's fine,
// we'll construct an appropriate error string from the body and status code.
_ = json.Unmarshal(b, structuredError)

structuredError.rawBody = string(b)
structuredError.StatusCode = resp.StatusCode
structuredError.request = resp.Request

return structuredError
}
5 changes: 5 additions & 0 deletions pkg/v1/remote/transport/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func TestCheckErrorNotError(t *testing.T) {
code: http.StatusBadRequest,
body: "",
msg: "unsupported status code 400",
}, {
code: http.StatusUnauthorized,
// Valid JSON, but not a structured error -- we should still print the body.
body: `{"details":"incorrect username or password"}`,
msg: `unsupported status code 401; body: {"details":"incorrect username or password"}`,
}, {
code: http.StatusUnauthorized,
body: "Not JSON",
Expand Down

0 comments on commit 72597da

Please sign in to comment.