errors.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package client
  2. import (
  3. "fmt"
  4. "github.com/docker/docker/api/types/versions"
  5. "github.com/pkg/errors"
  6. )
  7. // errConnectionFailed implements an error returned when connection failed.
  8. type errConnectionFailed struct {
  9. host string
  10. }
  11. // Error returns a string representation of an errConnectionFailed
  12. func (err errConnectionFailed) Error() string {
  13. if err.host == "" {
  14. return "Cannot connect to the Docker daemon. Is the docker daemon running on this host?"
  15. }
  16. return fmt.Sprintf("Cannot connect to the Docker daemon at %s. Is the docker daemon running?", err.host)
  17. }
  18. // IsErrConnectionFailed returns true if the error is caused by connection failed.
  19. func IsErrConnectionFailed(err error) bool {
  20. _, ok := errors.Cause(err).(errConnectionFailed)
  21. return ok
  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. type notFound interface {
  28. error
  29. NotFound() bool // Is the error a NotFound error
  30. }
  31. // IsErrNotFound returns true if the error is caused with an
  32. // object (image, container, network, volume, …) is not found in the docker host.
  33. func IsErrNotFound(err error) bool {
  34. te, ok := err.(notFound)
  35. return ok && te.NotFound()
  36. }
  37. // imageNotFoundError implements an error returned when an image is not in the docker host.
  38. type imageNotFoundError struct {
  39. imageID string
  40. }
  41. // NotFound indicates that this error type is of NotFound
  42. func (e imageNotFoundError) NotFound() bool {
  43. return true
  44. }
  45. // Error returns a string representation of an imageNotFoundError
  46. func (e imageNotFoundError) Error() string {
  47. return fmt.Sprintf("Error: No such image: %s", e.imageID)
  48. }
  49. // IsErrImageNotFound returns true if the error is caused
  50. // when an image is not found in the docker host.
  51. func IsErrImageNotFound(err error) bool {
  52. return IsErrNotFound(err)
  53. }
  54. // containerNotFoundError implements an error returned when a container is not in the docker host.
  55. type containerNotFoundError struct {
  56. containerID string
  57. }
  58. // NotFound indicates that this error type is of NotFound
  59. func (e containerNotFoundError) NotFound() bool {
  60. return true
  61. }
  62. // Error returns a string representation of a containerNotFoundError
  63. func (e containerNotFoundError) Error() string {
  64. return fmt.Sprintf("Error: No such container: %s", e.containerID)
  65. }
  66. // IsErrContainerNotFound returns true if the error is caused
  67. // when a container is not found in the docker host.
  68. func IsErrContainerNotFound(err error) bool {
  69. return IsErrNotFound(err)
  70. }
  71. // networkNotFoundError implements an error returned when a network is not in the docker host.
  72. type networkNotFoundError struct {
  73. networkID string
  74. }
  75. // NotFound indicates that this error type is of NotFound
  76. func (e networkNotFoundError) NotFound() bool {
  77. return true
  78. }
  79. // Error returns a string representation of a networkNotFoundError
  80. func (e networkNotFoundError) Error() string {
  81. return fmt.Sprintf("Error: No such network: %s", e.networkID)
  82. }
  83. // IsErrNetworkNotFound returns true if the error is caused
  84. // when a network is not found in the docker host.
  85. func IsErrNetworkNotFound(err error) bool {
  86. return IsErrNotFound(err)
  87. }
  88. // volumeNotFoundError implements an error returned when a volume is not in the docker host.
  89. type volumeNotFoundError struct {
  90. volumeID string
  91. }
  92. // NotFound indicates that this error type is of NotFound
  93. func (e volumeNotFoundError) NotFound() bool {
  94. return true
  95. }
  96. // Error returns a string representation of a volumeNotFoundError
  97. func (e volumeNotFoundError) Error() string {
  98. return fmt.Sprintf("Error: No such volume: %s", e.volumeID)
  99. }
  100. // IsErrVolumeNotFound returns true if the error is caused
  101. // when a volume is not found in the docker host.
  102. func IsErrVolumeNotFound(err error) bool {
  103. return IsErrNotFound(err)
  104. }
  105. // unauthorizedError represents an authorization error in a remote registry.
  106. type unauthorizedError struct {
  107. cause error
  108. }
  109. // Error returns a string representation of an unauthorizedError
  110. func (u unauthorizedError) Error() string {
  111. return u.cause.Error()
  112. }
  113. // IsErrUnauthorized returns true if the error is caused
  114. // when a remote registry authentication fails
  115. func IsErrUnauthorized(err error) bool {
  116. _, ok := err.(unauthorizedError)
  117. return ok
  118. }
  119. // nodeNotFoundError implements an error returned when a node is not found.
  120. type nodeNotFoundError struct {
  121. nodeID string
  122. }
  123. // Error returns a string representation of a nodeNotFoundError
  124. func (e nodeNotFoundError) Error() string {
  125. return fmt.Sprintf("Error: No such node: %s", e.nodeID)
  126. }
  127. // NotFound indicates that this error type is of NotFound
  128. func (e nodeNotFoundError) NotFound() bool {
  129. return true
  130. }
  131. // IsErrNodeNotFound returns true if the error is caused
  132. // when a node is not found.
  133. func IsErrNodeNotFound(err error) bool {
  134. _, ok := err.(nodeNotFoundError)
  135. return ok
  136. }
  137. // serviceNotFoundError implements an error returned when a service is not found.
  138. type serviceNotFoundError struct {
  139. serviceID string
  140. }
  141. // Error returns a string representation of a serviceNotFoundError
  142. func (e serviceNotFoundError) Error() string {
  143. return fmt.Sprintf("Error: No such service: %s", e.serviceID)
  144. }
  145. // NotFound indicates that this error type is of NotFound
  146. func (e serviceNotFoundError) NotFound() bool {
  147. return true
  148. }
  149. // IsErrServiceNotFound returns true if the error is caused
  150. // when a service is not found.
  151. func IsErrServiceNotFound(err error) bool {
  152. _, ok := err.(serviceNotFoundError)
  153. return ok
  154. }
  155. // taskNotFoundError implements an error returned when a task is not found.
  156. type taskNotFoundError struct {
  157. taskID string
  158. }
  159. // Error returns a string representation of a taskNotFoundError
  160. func (e taskNotFoundError) Error() string {
  161. return fmt.Sprintf("Error: No such task: %s", e.taskID)
  162. }
  163. // NotFound indicates that this error type is of NotFound
  164. func (e taskNotFoundError) NotFound() bool {
  165. return true
  166. }
  167. // IsErrTaskNotFound returns true if the error is caused
  168. // when a task is not found.
  169. func IsErrTaskNotFound(err error) bool {
  170. _, ok := err.(taskNotFoundError)
  171. return ok
  172. }
  173. type pluginPermissionDenied struct {
  174. name string
  175. }
  176. func (e pluginPermissionDenied) Error() string {
  177. return "Permission denied while installing plugin " + e.name
  178. }
  179. // IsErrPluginPermissionDenied returns true if the error is caused
  180. // when a user denies a plugin's permissions
  181. func IsErrPluginPermissionDenied(err error) bool {
  182. _, ok := err.(pluginPermissionDenied)
  183. return ok
  184. }
  185. // NewVersionError returns an error if the APIVersion required
  186. // if less than the current supported version
  187. func (cli *Client) NewVersionError(APIrequired, feature string) error {
  188. if cli.version != "" && versions.LessThan(cli.version, APIrequired) {
  189. return fmt.Errorf("%q requires API version %s, but the Docker daemon API version is %s", feature, APIrequired, cli.version)
  190. }
  191. return nil
  192. }
  193. // secretNotFoundError implements an error returned when a secret is not found.
  194. type secretNotFoundError struct {
  195. name string
  196. }
  197. // Error returns a string representation of a secretNotFoundError
  198. func (e secretNotFoundError) Error() string {
  199. return fmt.Sprintf("Error: no such secret: %s", e.name)
  200. }
  201. // NotFound indicates that this error type is of NotFound
  202. func (e secretNotFoundError) NotFound() bool {
  203. return true
  204. }
  205. // IsErrSecretNotFound returns true if the error is caused
  206. // when a secret is not found.
  207. func IsErrSecretNotFound(err error) bool {
  208. _, ok := err.(secretNotFoundError)
  209. return ok
  210. }
  211. // configNotFoundError implements an error returned when a config is not found.
  212. type configNotFoundError struct {
  213. name string
  214. }
  215. // Error returns a string representation of a configNotFoundError
  216. func (e configNotFoundError) Error() string {
  217. return fmt.Sprintf("Error: no such config: %s", e.name)
  218. }
  219. // NotFound indicates that this error type is of NotFound
  220. func (e configNotFoundError) NotFound() bool {
  221. return true
  222. }
  223. // IsErrConfigNotFound returns true if the error is caused
  224. // when a config is not found.
  225. func IsErrConfigNotFound(err error) bool {
  226. _, ok := err.(configNotFoundError)
  227. return ok
  228. }
  229. // pluginNotFoundError implements an error returned when a plugin is not in the docker host.
  230. type pluginNotFoundError struct {
  231. name string
  232. }
  233. // NotFound indicates that this error type is of NotFound
  234. func (e pluginNotFoundError) NotFound() bool {
  235. return true
  236. }
  237. // Error returns a string representation of a pluginNotFoundError
  238. func (e pluginNotFoundError) Error() string {
  239. return fmt.Sprintf("Error: No such plugin: %s", e.name)
  240. }
  241. // IsErrPluginNotFound returns true if the error is caused
  242. // when a plugin is not found in the docker host.
  243. func IsErrPluginNotFound(err error) bool {
  244. return IsErrNotFound(err)
  245. }