From ee2e149acae49a28c79c696363a8332a8ed1ae2c Mon Sep 17 00:00:00 2001 From: Boaz Shuster Date: Wed, 13 Dec 2017 22:46:55 +0200 Subject: [PATCH] Change errors structure to handle them gracefully in docker If commiting or reading Docker image fails the error message is verbose and thus it's hard to handle the error gracefully. This patch attempts to solve in the following way: The `docker/distribution/registry/client.HandleErrorResponse` takes a `http.Response` and returns a new error containing a human-readable error message alongside with the status code. In order to handle such errors, verify the error type and handle it: if respErr, ok := errors.Cause(err).(errcode.Error); ok { // handle the error here. } This change is going to apply on docker images. Signed-off-by: Boaz Shuster --- docker/docker_image_dest.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docker/docker_image_dest.go b/docker/docker_image_dest.go index 79c386225d..9a6c351378 100644 --- a/docker/docker_image_dest.go +++ b/docker/docker_image_dest.go @@ -131,7 +131,7 @@ func (d *dockerImageDestination) PutBlob(stream io.Reader, inputInfo types.BlobI defer res.Body.Close() if res.StatusCode != http.StatusAccepted { logrus.Debugf("Error initiating layer upload, response %#v", *res) - return types.BlobInfo{}, errors.Errorf("Error initiating layer upload to %s, status %d", uploadPath, res.StatusCode) + return types.BlobInfo{}, errors.Wrapf(client.HandleErrorResponse(res), "Error initiating layer upload to %s", uploadPath) } uploadLocation, err := res.Location() if err != nil { @@ -167,7 +167,7 @@ func (d *dockerImageDestination) PutBlob(stream io.Reader, inputInfo types.BlobI defer res.Body.Close() if res.StatusCode != http.StatusCreated { logrus.Debugf("Error uploading layer, response %#v", *res) - return types.BlobInfo{}, errors.Errorf("Error uploading layer to %s, status %d", uploadLocation, res.StatusCode) + return types.BlobInfo{}, errors.Wrapf(client.HandleErrorResponse(res), "Error uploading layer to %s", uploadLocation) } logrus.Debugf("Upload of layer %s complete", computedDigest) @@ -447,7 +447,7 @@ sigExists: logrus.Debugf("Error body %s", string(body)) } logrus.Debugf("Error uploading signature, status %d, %#v", res.StatusCode, res) - return errors.Errorf("Error uploading signature to %s, status %d", path, res.StatusCode) + return errors.Wrapf(client.HandleErrorResponse(res), "Error uploading signature to %s", path) } }