errors.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package client
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/docker/docker/api/types/versions"
  6. )
  7. // ErrConnectionFailed is an error raised when the connection between the client and the server failed.
  8. var ErrConnectionFailed = errors.New("Cannot connect to the Docker daemon. Is the docker daemon running on this host?")
  9. // ErrorConnectionFailed returns an error with host in the error message when connection to docker daemon failed.
  10. func ErrorConnectionFailed(host string) error {
  11. return fmt.Errorf("Cannot connect to the Docker daemon at %s. Is the docker daemon running?", host)
  12. }
  13. type notFound interface {
  14. error
  15. NotFound() bool // Is the error a NotFound error
  16. }
  17. // IsErrNotFound returns true if the error is caused with an
  18. // object (image, container, network, volume, …) is not found in the docker host.
  19. func IsErrNotFound(err error) bool {
  20. te, ok := err.(notFound)
  21. return ok && te.NotFound()
  22. }
  23. // imageNotFoundError implements an error returned when an image is not in the docker host.
  24. type imageNotFoundError struct {
  25. imageID string
  26. }
  27. // NotFound indicates that this error type is of NotFound
  28. func (e imageNotFoundError) NotFound() bool {
  29. return true
  30. }
  31. // Error returns a string representation of an imageNotFoundError
  32. func (e imageNotFoundError) Error() string {
  33. return fmt.Sprintf("Error: No such image: %s", e.imageID)
  34. }
  35. // IsErrImageNotFound returns true if the error is caused
  36. // when an image is not found in the docker host.
  37. func IsErrImageNotFound(err error) bool {
  38. return IsErrNotFound(err)
  39. }
  40. // containerNotFoundError implements an error returned when a container is not in the docker host.
  41. type containerNotFoundError struct {
  42. containerID string
  43. }
  44. // NotFound indicates that this error type is of NotFound
  45. func (e containerNotFoundError) NotFound() bool {
  46. return true
  47. }
  48. // Error returns a string representation of a containerNotFoundError
  49. func (e containerNotFoundError) Error() string {
  50. return fmt.Sprintf("Error: No such container: %s", e.containerID)
  51. }
  52. // IsErrContainerNotFound returns true if the error is caused
  53. // when a container is not found in the docker host.
  54. func IsErrContainerNotFound(err error) bool {
  55. return IsErrNotFound(err)
  56. }
  57. // networkNotFoundError implements an error returned when a network is not in the docker host.
  58. type networkNotFoundError struct {
  59. networkID string
  60. }
  61. // NotFound indicates that this error type is of NotFound
  62. func (e networkNotFoundError) NotFound() bool {
  63. return true
  64. }
  65. // Error returns a string representation of a networkNotFoundError
  66. func (e networkNotFoundError) Error() string {
  67. return fmt.Sprintf("Error: No such network: %s", e.networkID)
  68. }
  69. // IsErrNetworkNotFound returns true if the error is caused
  70. // when a network is not found in the docker host.
  71. func IsErrNetworkNotFound(err error) bool {
  72. return IsErrNotFound(err)
  73. }
  74. // volumeNotFoundError implements an error returned when a volume is not in the docker host.
  75. type volumeNotFoundError struct {
  76. volumeID string
  77. }
  78. // NotFound indicates that this error type is of NotFound
  79. func (e volumeNotFoundError) NotFound() bool {
  80. return true
  81. }
  82. // Error returns a string representation of a volumeNotFoundError
  83. func (e volumeNotFoundError) Error() string {
  84. return fmt.Sprintf("Error: No such volume: %s", e.volumeID)
  85. }
  86. // IsErrVolumeNotFound returns true if the error is caused
  87. // when a volume is not found in the docker host.
  88. func IsErrVolumeNotFound(err error) bool {
  89. return IsErrNotFound(err)
  90. }
  91. // unauthorizedError represents an authorization error in a remote registry.
  92. type unauthorizedError struct {
  93. cause error
  94. }
  95. // Error returns a string representation of an unauthorizedError
  96. func (u unauthorizedError) Error() string {
  97. return u.cause.Error()
  98. }
  99. // IsErrUnauthorized returns true if the error is caused
  100. // when a remote registry authentication fails
  101. func IsErrUnauthorized(err error) bool {
  102. _, ok := err.(unauthorizedError)
  103. return ok
  104. }
  105. // nodeNotFoundError implements an error returned when a node is not found.
  106. type nodeNotFoundError struct {
  107. nodeID string
  108. }
  109. // Error returns a string representation of a nodeNotFoundError
  110. func (e nodeNotFoundError) Error() string {
  111. return fmt.Sprintf("Error: No such node: %s", e.nodeID)
  112. }
  113. // NotFound indicates that this error type is of NotFound
  114. func (e nodeNotFoundError) NotFound() bool {
  115. return true
  116. }
  117. // IsErrNodeNotFound returns true if the error is caused
  118. // when a node is not found.
  119. func IsErrNodeNotFound(err error) bool {
  120. _, ok := err.(nodeNotFoundError)
  121. return ok
  122. }
  123. // serviceNotFoundError implements an error returned when a service is not found.
  124. type serviceNotFoundError struct {
  125. serviceID string
  126. }
  127. // Error returns a string representation of a serviceNotFoundError
  128. func (e serviceNotFoundError) Error() string {
  129. return fmt.Sprintf("Error: No such service: %s", e.serviceID)
  130. }
  131. // NotFound indicates that this error type is of NotFound
  132. func (e serviceNotFoundError) NotFound() bool {
  133. return true
  134. }
  135. // IsErrServiceNotFound returns true if the error is caused
  136. // when a service is not found.
  137. func IsErrServiceNotFound(err error) bool {
  138. _, ok := err.(serviceNotFoundError)
  139. return ok
  140. }
  141. // taskNotFoundError implements an error returned when a task is not found.
  142. type taskNotFoundError struct {
  143. taskID string
  144. }
  145. // Error returns a string representation of a taskNotFoundError
  146. func (e taskNotFoundError) Error() string {
  147. return fmt.Sprintf("Error: No such task: %s", e.taskID)
  148. }
  149. // NotFound indicates that this error type is of NotFound
  150. func (e taskNotFoundError) NotFound() bool {
  151. return true
  152. }
  153. // IsErrTaskNotFound returns true if the error is caused
  154. // when a task is not found.
  155. func IsErrTaskNotFound(err error) bool {
  156. _, ok := err.(taskNotFoundError)
  157. return ok
  158. }
  159. type pluginPermissionDenied struct {
  160. name string
  161. }
  162. func (e pluginPermissionDenied) Error() string {
  163. return "Permission denied while installing plugin " + e.name
  164. }
  165. // IsErrPluginPermissionDenied returns true if the error is caused
  166. // when a user denies a plugin's permissions
  167. func IsErrPluginPermissionDenied(err error) bool {
  168. _, ok := err.(pluginPermissionDenied)
  169. return ok
  170. }
  171. // NewVersionError returns an error if the APIVersion required
  172. // if less than the current supported version
  173. func (cli *Client) NewVersionError(APIrequired, feature string) error {
  174. if versions.LessThan(cli.version, APIrequired) {
  175. return fmt.Errorf("%q requires API version %s, but the Docker server is version %s", feature, APIrequired, cli.version)
  176. }
  177. return nil
  178. }