errors.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/registry/api/errcode"
  9. "github.com/docker/distribution/registry/api/v2"
  10. "github.com/docker/distribution/registry/client"
  11. "github.com/docker/distribution/registry/client/auth"
  12. "github.com/docker/docker/distribution/xfer"
  13. "github.com/docker/docker/reference"
  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. func translatePullError(err error, ref reference.Named) error {
  55. switch v := err.(type) {
  56. case errcode.Errors:
  57. if len(v) != 0 {
  58. for _, extra := range v[1:] {
  59. logrus.Infof("Ignoring extra error returned from registry: %v", extra)
  60. }
  61. return translatePullError(v[0], ref)
  62. }
  63. case errcode.Error:
  64. var newErr error
  65. switch v.Code {
  66. case errcode.ErrorCodeDenied:
  67. // ErrorCodeDenied is used when access to the repository was denied
  68. newErr = errors.Errorf("repository %s not found: does not exist or no read access", ref.Name())
  69. case v2.ErrorCodeManifestUnknown:
  70. newErr = errors.Errorf("manifest for %s not found", ref.String())
  71. case v2.ErrorCodeNameUnknown:
  72. newErr = errors.Errorf("repository %s not found", ref.Name())
  73. }
  74. if newErr != nil {
  75. logrus.Infof("Translating %q to %q", err, newErr)
  76. return newErr
  77. }
  78. case xfer.DoNotRetry:
  79. return translatePullError(v.Err, ref)
  80. }
  81. return err
  82. }
  83. // continueOnError returns true if we should fallback to the next endpoint
  84. // as a result of this error.
  85. func continueOnError(err error) bool {
  86. switch v := err.(type) {
  87. case errcode.Errors:
  88. if len(v) == 0 {
  89. return true
  90. }
  91. return continueOnError(v[0])
  92. case ErrNoSupport:
  93. return continueOnError(v.Err)
  94. case errcode.Error:
  95. return shouldV2Fallback(v)
  96. case *client.UnexpectedHTTPResponseError:
  97. return true
  98. case ImageConfigPullError:
  99. return false
  100. case error:
  101. return !strings.Contains(err.Error(), strings.ToLower(syscall.ENOSPC.Error()))
  102. }
  103. // let's be nice and fallback if the error is a completely
  104. // unexpected one.
  105. // If new errors have to be handled in some way, please
  106. // add them to the switch above.
  107. return true
  108. }
  109. // retryOnError wraps the error in xfer.DoNotRetry if we should not retry the
  110. // operation after this error.
  111. func retryOnError(err error) error {
  112. switch v := err.(type) {
  113. case errcode.Errors:
  114. if len(v) != 0 {
  115. return retryOnError(v[0])
  116. }
  117. case errcode.Error:
  118. switch v.Code {
  119. case errcode.ErrorCodeUnauthorized, errcode.ErrorCodeUnsupported, errcode.ErrorCodeDenied, errcode.ErrorCodeTooManyRequests, v2.ErrorCodeNameUnknown:
  120. return xfer.DoNotRetry{Err: err}
  121. }
  122. case *url.Error:
  123. switch v.Err {
  124. case auth.ErrNoBasicAuthCredentials, auth.ErrNoToken:
  125. return xfer.DoNotRetry{Err: v.Err}
  126. }
  127. return retryOnError(v.Err)
  128. case *client.UnexpectedHTTPResponseError:
  129. return xfer.DoNotRetry{Err: err}
  130. case error:
  131. if err == distribution.ErrBlobUnknown {
  132. return xfer.DoNotRetry{Err: err}
  133. }
  134. if strings.Contains(err.Error(), strings.ToLower(syscall.ENOSPC.Error())) {
  135. return xfer.DoNotRetry{Err: err}
  136. }
  137. }
  138. // let's be nice and fallback if the error is a completely
  139. // unexpected one.
  140. // If new errors have to be handled in some way, please
  141. // add them to the switch above.
  142. return err
  143. }