errors.go 6.0 KB

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