errors.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. // IsErrNotFound returns true if the error is a NotFound error, which is returned
  28. // by the API when some object is not found. It is an alias for [errdefs.IsNotFound].
  29. func IsErrNotFound(err error) bool {
  30. return errdefs.IsNotFound(err)
  31. }
  32. type objectNotFoundError struct {
  33. object string
  34. id string
  35. }
  36. func (e objectNotFoundError) NotFound() {}
  37. func (e objectNotFoundError) Error() string {
  38. return fmt.Sprintf("Error: No such %s: %s", e.object, e.id)
  39. }
  40. // NewVersionError returns an error if the APIVersion required
  41. // if less than the current supported version
  42. func (cli *Client) NewVersionError(APIrequired, feature string) error {
  43. if cli.version != "" && versions.LessThan(cli.version, APIrequired) {
  44. return fmt.Errorf("%q requires API version %s, but the Docker daemon API version is %s", feature, APIrequired, cli.version)
  45. }
  46. return nil
  47. }