Skip to content

Commit

Permalink
Merge pull request #5164 from errordeveloper/master
Browse files Browse the repository at this point in the history
Improve unexpected response error handling
  • Loading branch information
estesp committed Mar 14, 2021
2 parents 6f94b15 + d1b7784 commit 92009ad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion remotes/docker/pusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func (pw *pushWriter) Commit(ctx context.Context, size int64, expected digest.Di
switch resp.StatusCode {
case http.StatusOK, http.StatusCreated, http.StatusNoContent, http.StatusAccepted:
default:
return errors.Errorf("unexpected status: %s", resp.Status)
return remoteserrors.NewUnexpectedStatusErr(resp.Response)
}

status, err := pw.tracker.GetStatus(pw.ref)
Expand Down
18 changes: 14 additions & 4 deletions remotes/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ var _ error = ErrUnexpectedStatus{}

// ErrUnexpectedStatus is returned if a registry API request returned with unexpected HTTP status
type ErrUnexpectedStatus struct {
Status string
StatusCode int
Body []byte
Status string
StatusCode int
Body []byte
RequestURL, RequestMethod string
}

func (e ErrUnexpectedStatus) Error() string {
Expand All @@ -42,5 +43,14 @@ func NewUnexpectedStatusErr(resp *http.Response) error {
if resp.Body != nil {
b, _ = ioutil.ReadAll(io.LimitReader(resp.Body, 64000)) // 64KB
}
return ErrUnexpectedStatus{Status: resp.Status, StatusCode: resp.StatusCode, Body: b}
err := ErrUnexpectedStatus{
Body: b,
Status: resp.Status,
StatusCode: resp.StatusCode,
RequestMethod: resp.Request.Method,
}
if resp.Request.URL != nil {
err.RequestURL = resp.Request.URL.String()
}
return err
}

0 comments on commit 92009ad

Please sign in to comment.