errors.go 6.1 KB

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