errors.go 3.7 KB

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