errors.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "context"
  4. "fmt"
  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. // IsErrNotFound returns true if the error is a NotFound error, which is returned
  29. // by the API when some object is not found. It is an alias for [errdefs.IsNotFound].
  30. func IsErrNotFound(err error) bool {
  31. return errdefs.IsNotFound(err)
  32. }
  33. type objectNotFoundError struct {
  34. object string
  35. id string
  36. }
  37. func (e objectNotFoundError) NotFound() {}
  38. func (e objectNotFoundError) Error() string {
  39. return fmt.Sprintf("Error: No such %s: %s", e.object, e.id)
  40. }
  41. // NewVersionError returns an error if the APIVersion required is less than the
  42. // current supported version.
  43. //
  44. // It performs API-version negotiation if the Client is configured with this
  45. // option, otherwise it assumes the latest API version is used.
  46. func (cli *Client) NewVersionError(ctx context.Context, APIrequired, feature string) error {
  47. // Make sure we negotiated (if the client is configured to do so),
  48. // as code below contains API-version specific handling of options.
  49. //
  50. // Normally, version-negotiation (if enabled) would not happen until
  51. // the API request is made.
  52. cli.checkVersion(ctx)
  53. if cli.version != "" && versions.LessThan(cli.version, APIrequired) {
  54. return fmt.Errorf("%q requires API version %s, but the Docker daemon API version is %s", feature, APIrequired, cli.version)
  55. }
  56. return nil
  57. }