errors.go 5.5 KB

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