errors.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.err.Error()
  38. }
  39. // shouldV2Fallback returns true if this error is a reason to fall back to v1.
  40. func shouldV2Fallback(err errcode.Error) bool {
  41. switch err.Code {
  42. case errcode.ErrorCodeUnauthorized, v2.ErrorCodeManifestUnknown, v2.ErrorCodeNameUnknown:
  43. return true
  44. }
  45. return false
  46. }
  47. // continueOnError returns true if we should fallback to the next endpoint
  48. // as a result of this error.
  49. func continueOnError(err error) bool {
  50. switch v := err.(type) {
  51. case errcode.Errors:
  52. if len(v) == 0 {
  53. return true
  54. }
  55. return continueOnError(v[0])
  56. case ErrNoSupport:
  57. return continueOnError(v.Err)
  58. case errcode.Error:
  59. return shouldV2Fallback(v)
  60. case *client.UnexpectedHTTPResponseError:
  61. return true
  62. case ImageConfigPullError:
  63. return false
  64. case error:
  65. return !strings.Contains(err.Error(), strings.ToLower(syscall.ENOSPC.Error()))
  66. }
  67. // let's be nice and fallback if the error is a completely
  68. // unexpected one.
  69. // If new errors have to be handled in some way, please
  70. // add them to the switch above.
  71. return true
  72. }
  73. // retryOnError wraps the error in xfer.DoNotRetry if we should not retry the
  74. // operation after this error.
  75. func retryOnError(err error) error {
  76. switch v := err.(type) {
  77. case errcode.Errors:
  78. return retryOnError(v[0])
  79. case errcode.Error:
  80. switch v.Code {
  81. case errcode.ErrorCodeUnauthorized, errcode.ErrorCodeUnsupported, errcode.ErrorCodeDenied:
  82. return xfer.DoNotRetry{Err: err}
  83. }
  84. case *url.Error:
  85. if v.Err == auth.ErrNoBasicAuthCredentials {
  86. return xfer.DoNotRetry{Err: v.Err}
  87. }
  88. return retryOnError(v.Err)
  89. case *client.UnexpectedHTTPResponseError:
  90. return xfer.DoNotRetry{Err: err}
  91. case error:
  92. if strings.Contains(err.Error(), strings.ToLower(syscall.ENOSPC.Error())) {
  93. return xfer.DoNotRetry{Err: err}
  94. }
  95. }
  96. // let's be nice and fallback if the error is a completely
  97. // unexpected one.
  98. // If new errors have to be handled in some way, please
  99. // add them to the switch above.
  100. return err
  101. }