errors.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package 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. "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/sirupsen/logrus"
  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. type notFoundError struct {
  55. cause errcode.Error
  56. ref reference.Named
  57. }
  58. func (e notFoundError) Error() string {
  59. switch e.cause.Code {
  60. case errcode.ErrorCodeDenied:
  61. // ErrorCodeDenied is used when access to the repository was denied
  62. return fmt.Sprintf("pull access denied for %s, repository does not exist or may require 'docker login'", reference.FamiliarName(e.ref))
  63. case v2.ErrorCodeManifestUnknown:
  64. return fmt.Sprintf("manifest for %s not found", reference.FamiliarString(e.ref))
  65. case v2.ErrorCodeNameUnknown:
  66. return fmt.Sprintf("repository %s not found", reference.FamiliarName(e.ref))
  67. }
  68. // Shouldn't get here, but this is better than returning an empty string
  69. return e.cause.Message
  70. }
  71. func (e notFoundError) NotFound() {}
  72. func (e notFoundError) Cause() error {
  73. return e.cause
  74. }
  75. type unknownError struct {
  76. cause error
  77. }
  78. func (e unknownError) Error() string {
  79. return e.cause.Error()
  80. }
  81. func (e unknownError) Cause() error {
  82. return e.cause
  83. }
  84. func (e unknownError) Unknown() {}
  85. // TranslatePullError is used to convert an error from a registry pull
  86. // operation to an error representing the entire pull operation. Any error
  87. // information which is not used by the returned error gets output to
  88. // log at info level.
  89. func TranslatePullError(err error, ref reference.Named) error {
  90. switch v := err.(type) {
  91. case errcode.Errors:
  92. if len(v) != 0 {
  93. for _, extra := range v[1:] {
  94. logrus.Infof("Ignoring extra error returned from registry: %v", extra)
  95. }
  96. return TranslatePullError(v[0], ref)
  97. }
  98. case errcode.Error:
  99. switch v.Code {
  100. case errcode.ErrorCodeDenied, v2.ErrorCodeManifestUnknown, v2.ErrorCodeNameUnknown:
  101. return notFoundError{v, ref}
  102. }
  103. case xfer.DoNotRetry:
  104. return TranslatePullError(v.Err, ref)
  105. }
  106. return unknownError{err}
  107. }
  108. // continueOnError returns true if we should fallback to the next endpoint
  109. // as a result of this error.
  110. func continueOnError(err error) bool {
  111. switch v := err.(type) {
  112. case errcode.Errors:
  113. if len(v) == 0 {
  114. return true
  115. }
  116. return continueOnError(v[0])
  117. case ErrNoSupport:
  118. return continueOnError(v.Err)
  119. case errcode.Error:
  120. return shouldV2Fallback(v)
  121. case *client.UnexpectedHTTPResponseError:
  122. return true
  123. case ImageConfigPullError:
  124. return false
  125. case error:
  126. return !strings.Contains(err.Error(), strings.ToLower(syscall.ESRCH.Error()))
  127. }
  128. // let's be nice and fallback if the error is a completely
  129. // unexpected one.
  130. // If new errors have to be handled in some way, please
  131. // add them to the switch above.
  132. return true
  133. }
  134. // retryOnError wraps the error in xfer.DoNotRetry if we should not retry the
  135. // operation after this error.
  136. func retryOnError(err error) error {
  137. switch v := err.(type) {
  138. case errcode.Errors:
  139. if len(v) != 0 {
  140. return retryOnError(v[0])
  141. }
  142. case errcode.Error:
  143. switch v.Code {
  144. case errcode.ErrorCodeUnauthorized, errcode.ErrorCodeUnsupported, errcode.ErrorCodeDenied, errcode.ErrorCodeTooManyRequests, v2.ErrorCodeNameUnknown:
  145. return xfer.DoNotRetry{Err: err}
  146. }
  147. case *url.Error:
  148. switch v.Err {
  149. case auth.ErrNoBasicAuthCredentials, auth.ErrNoToken:
  150. return xfer.DoNotRetry{Err: v.Err}
  151. }
  152. return retryOnError(v.Err)
  153. case *client.UnexpectedHTTPResponseError:
  154. return xfer.DoNotRetry{Err: err}
  155. case error:
  156. if err == distribution.ErrBlobUnknown {
  157. return xfer.DoNotRetry{Err: err}
  158. }
  159. if strings.Contains(err.Error(), strings.ToLower(syscall.ENOSPC.Error())) {
  160. return xfer.DoNotRetry{Err: err}
  161. }
  162. }
  163. // let's be nice and fallback if the error is a completely
  164. // unexpected one.
  165. // If new errors have to be handled in some way, please
  166. // add them to the switch above.
  167. return err
  168. }
  169. type invalidManifestClassError struct {
  170. mediaType string
  171. class string
  172. }
  173. func (e invalidManifestClassError) Error() string {
  174. return fmt.Sprintf("Encountered remote %q(%s) when fetching", e.mediaType, e.class)
  175. }
  176. func (e invalidManifestClassError) InvalidParameter() {}
  177. type invalidManifestFormatError struct{}
  178. func (invalidManifestFormatError) Error() string {
  179. return "unsupported manifest format"
  180. }
  181. func (invalidManifestFormatError) InvalidParameter() {}
  182. type reservedNameError string
  183. func (e reservedNameError) Error() string {
  184. return "'" + string(e) + "' is a reserved name"
  185. }
  186. func (e reservedNameError) Forbidden() {}