errors.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. if errdefs.IsNotFound(err) {
  36. return true
  37. }
  38. var e notFound
  39. return errors.As(err, &e)
  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. // IsErrUnauthorized returns true if the error is caused
  50. // when a remote registry authentication fails
  51. //
  52. // Deprecated: use errdefs.IsUnauthorized
  53. func IsErrUnauthorized(err error) bool {
  54. return errdefs.IsUnauthorized(err)
  55. }
  56. type pluginPermissionDenied struct {
  57. name string
  58. }
  59. func (e pluginPermissionDenied) Error() string {
  60. return "Permission denied while installing plugin " + e.name
  61. }
  62. // IsErrNotImplemented returns true if the error is a NotImplemented error.
  63. // This is returned by the API when a requested feature has not been
  64. // implemented.
  65. //
  66. // Deprecated: use errdefs.IsNotImplemented
  67. func IsErrNotImplemented(err error) bool {
  68. return errdefs.IsNotImplemented(err)
  69. }
  70. // NewVersionError returns an error if the APIVersion required
  71. // if less than the current supported version
  72. func (cli *Client) NewVersionError(APIrequired, feature string) error {
  73. if cli.version != "" && versions.LessThan(cli.version, APIrequired) {
  74. return fmt.Errorf("%q requires API version %s, but the Docker daemon API version is %s", feature, APIrequired, cli.version)
  75. }
  76. return nil
  77. }