errors.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. type pluginPermissionDenied struct {
  50. name string
  51. }
  52. func (e pluginPermissionDenied) Error() string {
  53. return "Permission denied while installing plugin " + e.name
  54. }
  55. // NewVersionError returns an error if the APIVersion required
  56. // if less than the current supported version
  57. func (cli *Client) NewVersionError(APIrequired, feature string) error {
  58. if cli.version != "" && versions.LessThan(cli.version, APIrequired) {
  59. return fmt.Errorf("%q requires API version %s, but the Docker daemon API version is %s", feature, APIrequired, cli.version)
  60. }
  61. return nil
  62. }