github.com/sams1990/dockerrepo@v17.12.1-ce-rc2+incompatible/distribution/errors.go (about)

     1  package distribution
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"strings"
     7  	"syscall"
     8  
     9  	"github.com/docker/distribution"
    10  	"github.com/docker/distribution/reference"
    11  	"github.com/docker/distribution/registry/api/errcode"
    12  	"github.com/docker/distribution/registry/api/v2"
    13  	"github.com/docker/distribution/registry/client"
    14  	"github.com/docker/distribution/registry/client/auth"
    15  	"github.com/docker/docker/distribution/xfer"
    16  	"github.com/sirupsen/logrus"
    17  )
    18  
    19  // ErrNoSupport is an error type used for errors indicating that an operation
    20  // is not supported. It encapsulates a more specific error.
    21  type ErrNoSupport struct{ Err error }
    22  
    23  func (e ErrNoSupport) Error() string {
    24  	if e.Err == nil {
    25  		return "not supported"
    26  	}
    27  	return e.Err.Error()
    28  }
    29  
    30  // fallbackError wraps an error that can possibly allow fallback to a different
    31  // endpoint.
    32  type fallbackError struct {
    33  	// err is the error being wrapped.
    34  	err error
    35  	// confirmedV2 is set to true if it was confirmed that the registry
    36  	// supports the v2 protocol. This is used to limit fallbacks to the v1
    37  	// protocol.
    38  	confirmedV2 bool
    39  	// transportOK is set to true if we managed to speak HTTP with the
    40  	// registry. This confirms that we're using appropriate TLS settings
    41  	// (or lack of TLS).
    42  	transportOK bool
    43  }
    44  
    45  // Error renders the FallbackError as a string.
    46  func (f fallbackError) Error() string {
    47  	return f.Cause().Error()
    48  }
    49  
    50  func (f fallbackError) Cause() error {
    51  	return f.err
    52  }
    53  
    54  // shouldV2Fallback returns true if this error is a reason to fall back to v1.
    55  func shouldV2Fallback(err errcode.Error) bool {
    56  	switch err.Code {
    57  	case errcode.ErrorCodeUnauthorized, v2.ErrorCodeManifestUnknown, v2.ErrorCodeNameUnknown:
    58  		return true
    59  	}
    60  	return false
    61  }
    62  
    63  type notFoundError struct {
    64  	cause errcode.Error
    65  	ref   reference.Named
    66  }
    67  
    68  func (e notFoundError) Error() string {
    69  	switch e.cause.Code {
    70  	case errcode.ErrorCodeDenied:
    71  		// ErrorCodeDenied is used when access to the repository was denied
    72  		return fmt.Sprintf("pull access denied for %s, repository does not exist or may require 'docker login'", reference.FamiliarName(e.ref))
    73  	case v2.ErrorCodeManifestUnknown:
    74  		return fmt.Sprintf("manifest for %s not found", reference.FamiliarString(e.ref))
    75  	case v2.ErrorCodeNameUnknown:
    76  		return fmt.Sprintf("repository %s not found", reference.FamiliarName(e.ref))
    77  	}
    78  	// Shouldn't get here, but this is better than returning an empty string
    79  	return e.cause.Message
    80  }
    81  
    82  func (e notFoundError) NotFound() {}
    83  
    84  func (e notFoundError) Cause() error {
    85  	return e.cause
    86  }
    87  
    88  type unknownError struct {
    89  	cause error
    90  }
    91  
    92  func (e unknownError) Error() string {
    93  	return e.cause.Error()
    94  }
    95  
    96  func (e unknownError) Cause() error {
    97  	return e.cause
    98  }
    99  
   100  func (e unknownError) Unknown() {}
   101  
   102  // TranslatePullError is used to convert an error from a registry pull
   103  // operation to an error representing the entire pull operation. Any error
   104  // information which is not used by the returned error gets output to
   105  // log at info level.
   106  func TranslatePullError(err error, ref reference.Named) error {
   107  	switch v := err.(type) {
   108  	case errcode.Errors:
   109  		if len(v) != 0 {
   110  			for _, extra := range v[1:] {
   111  				logrus.Infof("Ignoring extra error returned from registry: %v", extra)
   112  			}
   113  			return TranslatePullError(v[0], ref)
   114  		}
   115  	case errcode.Error:
   116  		switch v.Code {
   117  		case errcode.ErrorCodeDenied, v2.ErrorCodeManifestUnknown, v2.ErrorCodeNameUnknown:
   118  			return notFoundError{v, ref}
   119  		}
   120  	case xfer.DoNotRetry:
   121  		return TranslatePullError(v.Err, ref)
   122  	}
   123  
   124  	return unknownError{err}
   125  }
   126  
   127  // continueOnError returns true if we should fallback to the next endpoint
   128  // as a result of this error.
   129  func continueOnError(err error, mirrorEndpoint bool) bool {
   130  	switch v := err.(type) {
   131  	case errcode.Errors:
   132  		if len(v) == 0 {
   133  			return true
   134  		}
   135  		return continueOnError(v[0], mirrorEndpoint)
   136  	case ErrNoSupport:
   137  		return continueOnError(v.Err, mirrorEndpoint)
   138  	case errcode.Error:
   139  		return mirrorEndpoint || shouldV2Fallback(v)
   140  	case *client.UnexpectedHTTPResponseError:
   141  		return true
   142  	case ImageConfigPullError:
   143  		// ImageConfigPullError only happens with v2 images, v1 fallback is
   144  		// unnecessary.
   145  		// Failures from a mirror endpoint should result in fallback to the
   146  		// canonical repo.
   147  		return mirrorEndpoint
   148  	case error:
   149  		return !strings.Contains(err.Error(), strings.ToLower(syscall.ESRCH.Error()))
   150  	}
   151  	// let's be nice and fallback if the error is a completely
   152  	// unexpected one.
   153  	// If new errors have to be handled in some way, please
   154  	// add them to the switch above.
   155  	return true
   156  }
   157  
   158  // retryOnError wraps the error in xfer.DoNotRetry if we should not retry the
   159  // operation after this error.
   160  func retryOnError(err error) error {
   161  	switch v := err.(type) {
   162  	case errcode.Errors:
   163  		if len(v) != 0 {
   164  			return retryOnError(v[0])
   165  		}
   166  	case errcode.Error:
   167  		switch v.Code {
   168  		case errcode.ErrorCodeUnauthorized, errcode.ErrorCodeUnsupported, errcode.ErrorCodeDenied, errcode.ErrorCodeTooManyRequests, v2.ErrorCodeNameUnknown:
   169  			return xfer.DoNotRetry{Err: err}
   170  		}
   171  	case *url.Error:
   172  		switch v.Err {
   173  		case auth.ErrNoBasicAuthCredentials, auth.ErrNoToken:
   174  			return xfer.DoNotRetry{Err: v.Err}
   175  		}
   176  		return retryOnError(v.Err)
   177  	case *client.UnexpectedHTTPResponseError:
   178  		return xfer.DoNotRetry{Err: err}
   179  	case error:
   180  		if err == distribution.ErrBlobUnknown {
   181  			return xfer.DoNotRetry{Err: err}
   182  		}
   183  		if strings.Contains(err.Error(), strings.ToLower(syscall.ENOSPC.Error())) {
   184  			return xfer.DoNotRetry{Err: err}
   185  		}
   186  	}
   187  	// let's be nice and fallback if the error is a completely
   188  	// unexpected one.
   189  	// If new errors have to be handled in some way, please
   190  	// add them to the switch above.
   191  	return err
   192  }
   193  
   194  type invalidManifestClassError struct {
   195  	mediaType string
   196  	class     string
   197  }
   198  
   199  func (e invalidManifestClassError) Error() string {
   200  	return fmt.Sprintf("Encountered remote %q(%s) when fetching", e.mediaType, e.class)
   201  }
   202  
   203  func (e invalidManifestClassError) InvalidParameter() {}
   204  
   205  type invalidManifestFormatError struct{}
   206  
   207  func (invalidManifestFormatError) Error() string {
   208  	return "unsupported manifest format"
   209  }
   210  
   211  func (invalidManifestFormatError) InvalidParameter() {}
   212  
   213  type reservedNameError string
   214  
   215  func (e reservedNameError) Error() string {
   216  	return "'" + string(e) + "' is a reserved name"
   217  }
   218  
   219  func (e reservedNameError) Forbidden() {}