errors.go 5.9 KB

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