errors.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "fmt"
  4. "github.com/docker/docker/api/types/versions"
  5. "github.com/docker/docker/errdefs"
  6. "github.com/pkg/errors"
  7. )
  8. // errConnectionFailed implements an error returned when connection failed.
  9. type errConnectionFailed struct {
  10. host string
  11. }
  12. // Error returns a string representation of an errConnectionFailed
  13. func (err errConnectionFailed) Error() string {
  14. if err.host == "" {
  15. return "Cannot connect to the Docker daemon. Is the docker daemon running on this host?"
  16. }
  17. return fmt.Sprintf("Cannot connect to the Docker daemon at %s. Is the docker daemon running?", err.host)
  18. }
  19. // IsErrConnectionFailed returns true if the error is caused by connection failed.
  20. func IsErrConnectionFailed(err error) bool {
  21. return errors.As(err, &errConnectionFailed{})
  22. }
  23. // ErrorConnectionFailed returns an error with host in the error message when connection to docker daemon failed.
  24. func ErrorConnectionFailed(host string) error {
  25. return errConnectionFailed{host: host}
  26. }
  27. // Deprecated: use the errdefs.NotFound() interface instead. Kept for backward compatibility
  28. type notFound interface {
  29. error
  30. NotFound() bool
  31. }
  32. // IsErrNotFound returns true if the error is a NotFound error, which is returned
  33. // by the API when some object is not found.
  34. func IsErrNotFound(err error) bool {
  35. var e notFound
  36. if errors.As(err, &e) {
  37. return true
  38. }
  39. return errdefs.IsNotFound(err)
  40. }
  41. type objectNotFoundError struct {
  42. object string
  43. id string
  44. }
  45. func (e objectNotFoundError) NotFound() {}
  46. func (e objectNotFoundError) Error() string {
  47. return fmt.Sprintf("Error: No such %s: %s", e.object, e.id)
  48. }
  49. // unauthorizedError represents an authorization error in a remote registry.
  50. type unauthorizedError struct {
  51. cause error
  52. }
  53. // Error returns a string representation of an unauthorizedError
  54. func (u unauthorizedError) Error() string {
  55. return u.cause.Error()
  56. }
  57. // IsErrUnauthorized returns true if the error is caused
  58. // when a remote registry authentication fails
  59. func IsErrUnauthorized(err error) bool {
  60. if _, ok := err.(unauthorizedError); ok {
  61. return ok
  62. }
  63. return errdefs.IsUnauthorized(err)
  64. }
  65. type pluginPermissionDenied struct {
  66. name string
  67. }
  68. func (e pluginPermissionDenied) Error() string {
  69. return "Permission denied while installing plugin " + e.name
  70. }
  71. // IsErrPluginPermissionDenied returns true if the error is caused
  72. // when a user denies a plugin's permissions
  73. func IsErrPluginPermissionDenied(err error) bool {
  74. _, ok := err.(pluginPermissionDenied)
  75. return ok
  76. }
  77. type notImplementedError struct {
  78. message string
  79. }
  80. func (e notImplementedError) Error() string {
  81. return e.message
  82. }
  83. func (e notImplementedError) NotImplemented() bool {
  84. return true
  85. }
  86. // IsErrNotImplemented returns true if the error is a NotImplemented error.
  87. // This is returned by the API when a requested feature has not been
  88. // implemented.
  89. func IsErrNotImplemented(err error) bool {
  90. if _, ok := err.(notImplementedError); ok {
  91. return ok
  92. }
  93. return errdefs.IsNotImplemented(err)
  94. }
  95. // NewVersionError returns an error if the APIVersion required
  96. // if less than the current supported version
  97. func (cli *Client) NewVersionError(APIrequired, feature string) error {
  98. if cli.version != "" && versions.LessThan(cli.version, APIrequired) {
  99. return fmt.Errorf("%q requires API version %s, but the Docker daemon API version is %s", feature, APIrequired, cli.version)
  100. }
  101. return nil
  102. }