Skip to content

Commit

Permalink
return resp error message
Browse files Browse the repository at this point in the history
follow containers#709
return and wrap http response message to show server-side error message.

Signed-off-by: Qi Wang <[email protected]>
  • Loading branch information
QiWang19 committed Oct 24, 2019
1 parent b8d780c commit 9c14d60
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docker/docker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ func (c *dockerClient) getBearerToken(ctx context.Context, challenge challenge,
case http.StatusUnauthorized:
err := clientLib.HandleErrorResponse(res)
logrus.Debugf("Server response when trying to obtain an access token: \n%q", err.Error())
return nil, ErrUnauthorizedForCredentials
return nil, ErrUnauthorizedForCredentials{Err: err}
case http.StatusOK:
break
default:
Expand Down
16 changes: 13 additions & 3 deletions docker/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,30 @@ package docker

import (
"errors"
"fmt"
"net/http"

"github.com/docker/distribution/registry/client"
perrors "github.com/pkg/errors"
)

var (
// ErrV1NotSupported is returned when we're trying to talk to a
// docker V1 registry.
ErrV1NotSupported = errors.New("can't talk to a V1 docker registry")
// ErrUnauthorizedForCredentials is returned when the status code returned is 401
ErrUnauthorizedForCredentials = errors.New("unable to retrieve auth token: invalid username/password")
// ErrTooManyRequests is returned when the status code returned is 429
ErrTooManyRequests = errors.New("too many request to registry")
)

// ErrUnauthorizedForCredentials is returned when the status code returned is 401
type ErrUnauthorizedForCredentials struct { // We only use a struct to allow a type assertion, without limiting the contents of the error otherwise.
Err error
}

func (e ErrUnauthorizedForCredentials) Error() string {
return fmt.Sprintf("unable to retrieve auth token: invalid username/password: %s", e.Err.Error())
}

// httpResponseToError translates the https.Response into an error. It returns
// nil if the response is not considered an error.
func httpResponseToError(res *http.Response) error {
Expand All @@ -26,7 +35,8 @@ func httpResponseToError(res *http.Response) error {
case http.StatusTooManyRequests:
return ErrTooManyRequests
case http.StatusUnauthorized:
return ErrUnauthorizedForCredentials
err := client.HandleErrorResponse(res)
return ErrUnauthorizedForCredentials{Err: err}
default:
return perrors.Errorf("invalid status code from registry %d (%s)", res.StatusCode, http.StatusText(res.StatusCode))
}
Expand Down

0 comments on commit 9c14d60

Please sign in to comment.