github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/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/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 _, ok := errors.Cause(err).(errConnectionFailed) 27 return ok 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 type notFound interface { 36 error 37 NotFound() bool // Is the error a NotFound error 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 te, ok := err.(notFound) 44 return ok && te.NotFound() 45 } 46 47 type objectNotFoundError struct { 48 object string 49 id string 50 } 51 52 func (e objectNotFoundError) NotFound() bool { 53 return true 54 } 55 56 func (e objectNotFoundError) Error() string { 57 return fmt.Sprintf("Error: No such %s: %s", e.object, e.id) 58 } 59 60 func wrapResponseError(err error, resp serverResponse, object, id string) error { 61 switch { 62 case err == nil: 63 return nil 64 case resp.statusCode == http.StatusNotFound: 65 return objectNotFoundError{object: object, id: id} 66 case resp.statusCode == http.StatusNotImplemented: 67 return notImplementedError{message: err.Error()} 68 default: 69 return err 70 } 71 } 72 73 // unauthorizedError represents an authorization error in a remote registry. 74 type unauthorizedError struct { 75 cause error 76 } 77 78 // Error returns a string representation of an unauthorizedError 79 func (u unauthorizedError) Error() string { 80 return u.cause.Error() 81 } 82 83 // IsErrUnauthorized returns true if the error is caused 84 // when a remote registry authentication fails 85 func IsErrUnauthorized(err error) bool { 86 _, ok := err.(unauthorizedError) 87 return ok 88 } 89 90 type pluginPermissionDenied struct { 91 name string 92 } 93 94 func (e pluginPermissionDenied) Error() string { 95 return "Permission denied while installing plugin " + e.name 96 } 97 98 // IsErrPluginPermissionDenied returns true if the error is caused 99 // when a user denies a plugin's permissions 100 func IsErrPluginPermissionDenied(err error) bool { 101 _, ok := err.(pluginPermissionDenied) 102 return ok 103 } 104 105 type notImplementedError struct { 106 message string 107 } 108 109 func (e notImplementedError) Error() string { 110 return e.message 111 } 112 113 func (e notImplementedError) NotImplemented() bool { 114 return true 115 } 116 117 // IsErrNotImplemented returns true if the error is a NotImplemented error. 118 // This is returned by the API when a requested feature has not been 119 // implemented. 120 func IsErrNotImplemented(err error) bool { 121 te, ok := err.(notImplementedError) 122 return ok && te.NotImplemented() 123 } 124 125 // NewVersionError returns an error if the APIVersion required 126 // if less than the current supported version 127 func (cli *Client) NewVersionError(APIrequired, feature string) error { 128 if cli.version != "" && versions.LessThan(cli.version, APIrequired) { 129 return fmt.Errorf("%q requires API version %s, but the Docker daemon API version is %s", feature, APIrequired, cli.version) 130 } 131 return nil 132 }