errors.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package distribution
  2. import (
  3. "net/url"
  4. "strings"
  5. "syscall"
  6. "github.com/Sirupsen/logrus"
  7. "github.com/docker/distribution"
  8. "github.com/docker/distribution/reference"
  9. "github.com/docker/distribution/registry/api/errcode"
  10. "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/pkg/errors"
  15. )
  16. // ErrNoSupport is an error type used for errors indicating that an operation
  17. // is not supported. It encapsulates a more specific error.
  18. type ErrNoSupport struct{ Err error }
  19. func (e ErrNoSupport) Error() string {
  20. if e.Err == nil {
  21. return "not supported"
  22. }
  23. return e.Err.Error()
  24. }
  25. // fallbackError wraps an error that can possibly allow fallback to a different
  26. // endpoint.
  27. type fallbackError struct {
  28. // err is the error being wrapped.
  29. err error
  30. // confirmedV2 is set to true if it was confirmed that the registry
  31. // supports the v2 protocol. This is used to limit fallbacks to the v1
  32. // protocol.
  33. confirmedV2 bool
  34. // transportOK is set to true if we managed to speak HTTP with the
  35. // registry. This confirms that we're using appropriate TLS settings
  36. // (or lack of TLS).
  37. transportOK bool
  38. }
  39. // Error renders the FallbackError as a string.
  40. func (f fallbackError) Error() string {
  41. return f.Cause().Error()
  42. }
  43. func (f fallbackError) Cause() error {
  44. return f.err
  45. }
  46. // shouldV2Fallback returns true if this error is a reason to fall back to v1.
  47. func shouldV2Fallback(err errcode.Error) bool {
  48. switch err.Code {
  49. case errcode.ErrorCodeUnauthorized, v2.ErrorCodeManifestUnknown, v2.ErrorCodeNameUnknown:
  50. return true
  51. }
  52. return false
  53. }
  54. // TranslatePullError is used to convert an error from a registry pull
  55. // operation to an error representing the entire pull operation. Any error
  56. // information which is not used by the returned error gets output to
  57. // log at info level.
  58. func TranslatePullError(err error, ref reference.Named) error {
  59. switch v := err.(type) {
  60. case errcode.Errors:
  61. if len(v) != 0 {
  62. for _, extra := range v[1:] {
  63. logrus.Infof("Ignoring extra error returned from registry: %v", extra)
  64. }
  65. return TranslatePullError(v[0], ref)
  66. }
  67. case errcode.Error:
  68. var newErr error
  69. switch v.Code {
  70. case errcode.ErrorCodeDenied:
  71. // ErrorCodeDenied is used when access to the repository was denied
  72. newErr = errors.Errorf("repository %s not found: does not exist or no pull access", reference.FamiliarName(ref))
  73. case v2.ErrorCodeManifestUnknown:
  74. newErr = errors.Errorf("manifest for %s not found", reference.FamiliarString(ref))
  75. case v2.ErrorCodeNameUnknown:
  76. newErr = errors.Errorf("repository %s not found", reference.FamiliarName(ref))
  77. }
  78. if newErr != nil {
  79. logrus.Infof("Translating %q to %q", err, newErr)
  80. return newErr
  81. }
  82. case xfer.DoNotRetry:
  83. return TranslatePullError(v.Err, ref)
  84. }
  85. return err
  86. }
  87. // continueOnError returns true if we should fallback to the next endpoint
  88. // as a result of this error.
  89. func continueOnError(err error) bool {
  90. switch v := err.(type) {
  91. case errcode.Errors:
  92. if len(v) == 0 {
  93. return true
  94. }
  95. return continueOnError(v[0])
  96. case ErrNoSupport:
  97. return continueOnError(v.Err)
  98. case errcode.Error:
  99. return shouldV2Fallback(v)
  100. case *client.UnexpectedHTTPResponseError:
  101. return true
  102. case ImageConfigPullError:
  103. return false
  104. case error:
  105. return !strings.Contains(err.Error(), strings.ToLower(syscall.ENOSPC.Error()))
  106. }
  107. // let's be nice and fallback if the error is a completely
  108. // unexpected one.
  109. // If new errors have to be handled in some way, please
  110. // add them to the switch above.
  111. return true
  112. }
  113. // retryOnError wraps the error in xfer.DoNotRetry if we should not retry the
  114. // operation after this error.
  115. func retryOnError(err error) error {
  116. switch v := err.(type) {
  117. case errcode.Errors:
  118. if len(v) != 0 {
  119. return retryOnError(v[0])
  120. }
  121. case errcode.Error:
  122. switch v.Code {
  123. case errcode.ErrorCodeUnauthorized, errcode.ErrorCodeUnsupported, errcode.ErrorCodeDenied, errcode.ErrorCodeTooManyRequests, v2.ErrorCodeNameUnknown:
  124. return xfer.DoNotRetry{Err: err}
  125. }
  126. case *url.Error:
  127. switch v.Err {
  128. case auth.ErrNoBasicAuthCredentials, auth.ErrNoToken:
  129. return xfer.DoNotRetry{Err: v.Err}
  130. }
  131. return retryOnError(v.Err)
  132. case *client.UnexpectedHTTPResponseError:
  133. return xfer.DoNotRetry{Err: err}
  134. case error:
  135. if err == distribution.ErrBlobUnknown {
  136. return xfer.DoNotRetry{Err: err}
  137. }
  138. if strings.Contains(err.Error(), strings.ToLower(syscall.ENOSPC.Error())) {
  139. return xfer.DoNotRetry{Err: err}
  140. }
  141. }
  142. // let's be nice and fallback if the error is a completely
  143. // unexpected one.
  144. // If new errors have to be handled in some way, please
  145. // add them to the switch above.
  146. return err
  147. }