errors.go 4.3 KB

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