errors.go 5.6 KB

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