github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/client/errors.go (about) 1 package client // import "github.com/docker/docker/client" 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "github.com/docker/docker/api/types/versions" 8 "github.com/docker/docker/errdefs" 9 "github.com/pkg/errors" 10 ) 11 12 // errConnectionFailed implements an error returned when connection failed. 13 type errConnectionFailed struct { 14 host string 15 } 16 17 // Error returns a string representation of an errConnectionFailed 18 func (err errConnectionFailed) Error() string { 19 if err.host == "" { 20 return "Cannot connect to the Docker daemon. Is the docker daemon running on this host?" 21 } 22 return fmt.Sprintf("Cannot connect to the Docker daemon at %s. Is the docker daemon running?", err.host) 23 } 24 25 // IsErrConnectionFailed returns true if the error is caused by connection failed. 26 func IsErrConnectionFailed(err error) bool { 27 return errors.As(err, &errConnectionFailed{}) 28 } 29 30 // ErrorConnectionFailed returns an error with host in the error message when connection to docker daemon failed. 31 func ErrorConnectionFailed(host string) error { 32 return errConnectionFailed{host: host} 33 } 34 35 // Deprecated: use the errdefs.NotFound() interface instead. Kept for backward compatibility 36 type notFound interface { 37 error 38 NotFound() bool 39 } 40 41 // IsErrNotFound returns true if the error is a NotFound error, which is returned 42 // by the API when some object is not found. 43 func IsErrNotFound(err error) bool { 44 var e notFound 45 if errors.As(err, &e) { 46 return true 47 } 48 return errdefs.IsNotFound(err) 49 } 50 51 type objectNotFoundError struct { 52 object string 53 id string 54 } 55 56 func (e objectNotFoundError) NotFound() {} 57 58 func (e objectNotFoundError) Error() string { 59 return fmt.Sprintf("Error: No such %s: %s", e.object, e.id) 60 } 61 62 func wrapResponseError(err error, resp serverResponse, object, id string) error { 63 switch { 64 case err == nil: 65 return nil 66 case resp.statusCode == http.StatusNotFound: 67 return objectNotFoundError{object: object, id: id} 68 case resp.statusCode == http.StatusNotImplemented: 69 return errdefs.NotImplemented(err) 70 default: 71 return err 72 } 73 } 74 75 // unauthorizedError represents an authorization error in a remote registry. 76 type unauthorizedError struct { 77 cause error 78 } 79 80 // Error returns a string representation of an unauthorizedError 81 func (u unauthorizedError) Error() string { 82 return u.cause.Error() 83 } 84 85 // IsErrUnauthorized returns true if the error is caused 86 // when a remote registry authentication fails 87 func IsErrUnauthorized(err error) bool { 88 if _, ok := err.(unauthorizedError); ok { 89 return ok 90 } 91 return errdefs.IsUnauthorized(err) 92 } 93 94 type pluginPermissionDenied struct { 95 name string 96 } 97 98 func (e pluginPermissionDenied) Error() string { 99 return "Permission denied while installing plugin " + e.name 100 } 101 102 // IsErrPluginPermissionDenied returns true if the error is caused 103 // when a user denies a plugin's permissions 104 func IsErrPluginPermissionDenied(err error) bool { 105 _, ok := err.(pluginPermissionDenied) 106 return ok 107 } 108 109 type notImplementedError struct { 110 message string 111 } 112 113 func (e notImplementedError) Error() string { 114 return e.message 115 } 116 117 func (e notImplementedError) NotImplemented() bool { 118 return true 119 } 120 121 // IsErrNotImplemented returns true if the error is a NotImplemented error. 122 // This is returned by the API when a requested feature has not been 123 // implemented. 124 func IsErrNotImplemented(err error) bool { 125 if _, ok := err.(notImplementedError); ok { 126 return ok 127 } 128 return errdefs.IsNotImplemented(err) 129 } 130 131 // NewVersionError returns an error if the APIVersion required 132 // if less than the current supported version 133 func (cli *Client) NewVersionError(APIrequired, feature string) error { 134 if cli.version != "" && versions.LessThan(cli.version, APIrequired) { 135 return fmt.Errorf("%q requires API version %s, but the Docker daemon API version is %s", feature, APIrequired, cli.version) 136 } 137 return nil 138 }