Skip to content

Commit 92009ad

Browse files
authored
Merge pull request #5164 from errordeveloper/master
Improve unexpected response error handling
2 parents 6f94b15 + d1b7784 commit 92009ad

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

remotes/docker/pusher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func (pw *pushWriter) Commit(ctx context.Context, size int64, expected digest.Di
354354
switch resp.StatusCode {
355355
case http.StatusOK, http.StatusCreated, http.StatusNoContent, http.StatusAccepted:
356356
default:
357-
return errors.Errorf("unexpected status: %s", resp.Status)
357+
return remoteserrors.NewUnexpectedStatusErr(resp.Response)
358358
}
359359

360360
status, err := pw.tracker.GetStatus(pw.ref)

remotes/errors/errors.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ var _ error = ErrUnexpectedStatus{}
2727

2828
// ErrUnexpectedStatus is returned if a registry API request returned with unexpected HTTP status
2929
type ErrUnexpectedStatus struct {
30-
Status string
31-
StatusCode int
32-
Body []byte
30+
Status string
31+
StatusCode int
32+
Body []byte
33+
RequestURL, RequestMethod string
3334
}
3435

3536
func (e ErrUnexpectedStatus) Error() string {
@@ -42,5 +43,14 @@ func NewUnexpectedStatusErr(resp *http.Response) error {
4243
if resp.Body != nil {
4344
b, _ = ioutil.ReadAll(io.LimitReader(resp.Body, 64000)) // 64KB
4445
}
45-
return ErrUnexpectedStatus{Status: resp.Status, StatusCode: resp.StatusCode, Body: b}
46+
err := ErrUnexpectedStatus{
47+
Body: b,
48+
Status: resp.Status,
49+
StatusCode: resp.StatusCode,
50+
RequestMethod: resp.Request.Method,
51+
}
52+
if resp.Request.URL != nil {
53+
err.RequestURL = resp.Request.URL.String()
54+
}
55+
return err
4656
}

0 commit comments

Comments
 (0)