github.com/rawahars/moby@v24.0.4+incompatible/client/errors.go (about) 1 package client // import "github.com/docker/docker/client" 2 3 import ( 4 "fmt" 5 6 "github.com/docker/docker/api/types/versions" 7 "github.com/docker/docker/errdefs" 8 "github.com/pkg/errors" 9 ) 10 11 // errConnectionFailed implements an error returned when connection failed. 12 type errConnectionFailed struct { 13 host string 14 } 15 16 // Error returns a string representation of an errConnectionFailed 17 func (err errConnectionFailed) Error() string { 18 if err.host == "" { 19 return "Cannot connect to the Docker daemon. Is the docker daemon running on this host?" 20 } 21 return fmt.Sprintf("Cannot connect to the Docker daemon at %s. Is the docker daemon running?", err.host) 22 } 23 24 // IsErrConnectionFailed returns true if the error is caused by connection failed. 25 func IsErrConnectionFailed(err error) bool { 26 return errors.As(err, &errConnectionFailed{}) 27 } 28 29 // ErrorConnectionFailed returns an error with host in the error message when connection to docker daemon failed. 30 func ErrorConnectionFailed(host string) error { 31 return errConnectionFailed{host: host} 32 } 33 34 // Deprecated: use the errdefs.NotFound() interface instead. Kept for backward compatibility 35 type notFound interface { 36 error 37 NotFound() bool 38 } 39 40 // IsErrNotFound returns true if the error is a NotFound error, which is returned 41 // by the API when some object is not found. 42 func IsErrNotFound(err error) bool { 43 if errdefs.IsNotFound(err) { 44 return true 45 } 46 var e notFound 47 return errors.As(err, &e) 48 } 49 50 type objectNotFoundError struct { 51 object string 52 id string 53 } 54 55 func (e objectNotFoundError) NotFound() {} 56 57 func (e objectNotFoundError) Error() string { 58 return fmt.Sprintf("Error: No such %s: %s", e.object, e.id) 59 } 60 61 // NewVersionError returns an error if the APIVersion required 62 // if less than the current supported version 63 func (cli *Client) NewVersionError(APIrequired, feature string) error { 64 if cli.version != "" && versions.LessThan(cli.version, APIrequired) { 65 return fmt.Errorf("%q requires API version %s, but the Docker daemon API version is %s", feature, APIrequired, cli.version) 66 } 67 return nil 68 }