errors.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package distribution
  2. import (
  3. "net/url"
  4. "strings"
  5. "syscall"
  6. "github.com/docker/distribution/registry/api/errcode"
  7. "github.com/docker/distribution/registry/api/v2"
  8. "github.com/docker/distribution/registry/client"
  9. "github.com/docker/distribution/registry/client/auth"
  10. "github.com/docker/docker/distribution/xfer"
  11. )
  12. // ErrNoSupport is an error type used for errors indicating that an operation
  13. // is not supported. It encapsulates a more specific error.
  14. type ErrNoSupport struct{ Err error }
  15. func (e ErrNoSupport) Error() string {
  16. if e.Err == nil {
  17. return "not supported"
  18. }
  19. return e.Err.Error()
  20. }
  21. // fallbackError wraps an error that can possibly allow fallback to a different
  22. // endpoint.
  23. type fallbackError struct {
  24. // err is the error being wrapped.
  25. err error
  26. // confirmedV2 is set to true if it was confirmed that the registry
  27. // supports the v2 protocol. This is used to limit fallbacks to the v1
  28. // protocol.
  29. confirmedV2 bool
  30. // transportOK is set to true if we managed to speak HTTP with the
  31. // registry. This confirms that we're using appropriate TLS settings
  32. // (or lack of TLS).
  33. transportOK bool
  34. }
  35. // Error renders the FallbackError as a string.
  36. func (f fallbackError) Error() string {
  37. return f.Cause().Error()
  38. }
  39. func (f fallbackError) Cause() error {
  40. return f.err
  41. }
  42. // shouldV2Fallback returns true if this error is a reason to fall back to v1.
  43. func shouldV2Fallback(err errcode.Error) bool {
  44. switch err.Code {
  45. case errcode.ErrorCodeUnauthorized, v2.ErrorCodeManifestUnknown, v2.ErrorCodeNameUnknown:
  46. return true
  47. }
  48. return false
  49. }
  50. // continueOnError returns true if we should fallback to the next endpoint
  51. // as a result of this error.
  52. func continueOnError(err error) bool {
  53. switch v := err.(type) {
  54. case errcode.Errors:
  55. if len(v) == 0 {
  56. return true
  57. }
  58. return continueOnError(v[0])
  59. case ErrNoSupport:
  60. return continueOnError(v.Err)
  61. case errcode.Error:
  62. return shouldV2Fallback(v)
  63. case *client.UnexpectedHTTPResponseError:
  64. return true
  65. case ImageConfigPullError:
  66. return false
  67. case error:
  68. return !strings.Contains(err.Error(), strings.ToLower(syscall.ENOSPC.Error()))
  69. }
  70. // let's be nice and fallback if the error is a completely
  71. // unexpected one.
  72. // If new errors have to be handled in some way, please
  73. // add them to the switch above.
  74. return true
  75. }
  76. // retryOnError wraps the error in xfer.DoNotRetry if we should not retry the
  77. // operation after this error.
  78. func retryOnError(err error) error {
  79. switch v := err.(type) {
  80. case errcode.Errors:
  81. if len(v) != 0 {
  82. return retryOnError(v[0])
  83. }
  84. case errcode.Error:
  85. switch v.Code {
  86. case errcode.ErrorCodeUnauthorized, errcode.ErrorCodeUnsupported, errcode.ErrorCodeDenied, errcode.ErrorCodeTooManyRequests, v2.ErrorCodeNameUnknown:
  87. return xfer.DoNotRetry{Err: err}
  88. }
  89. case *url.Error:
  90. switch v.Err {
  91. case auth.ErrNoBasicAuthCredentials, auth.ErrNoToken:
  92. return xfer.DoNotRetry{Err: v.Err}
  93. }
  94. return retryOnError(v.Err)
  95. case *client.UnexpectedHTTPResponseError:
  96. return xfer.DoNotRetry{Err: err}
  97. case error:
  98. if strings.Contains(err.Error(), strings.ToLower(syscall.ENOSPC.Error())) {
  99. return xfer.DoNotRetry{Err: err}
  100. }
  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 err
  107. }