errors.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. default:
  55. return err
  56. }
  57. }
  58. // IsErrImageNotFound returns true if the error is caused
  59. // when an image is not found in the docker host.
  60. //
  61. // Deprecated: Use IsErrNotFound
  62. func IsErrImageNotFound(err error) bool {
  63. return IsErrNotFound(err)
  64. }
  65. // IsErrContainerNotFound returns true if the error is caused
  66. // when a container is not found in the docker host.
  67. //
  68. // Deprecated: Use IsErrNotFound
  69. func IsErrContainerNotFound(err error) bool {
  70. return IsErrNotFound(err)
  71. }
  72. // IsErrNetworkNotFound returns true if the error is caused
  73. // when a network is not found in the docker host.
  74. //
  75. // Deprecated: Use IsErrNotFound
  76. func IsErrNetworkNotFound(err error) bool {
  77. return IsErrNotFound(err)
  78. }
  79. // IsErrVolumeNotFound returns true if the error is caused
  80. // when a volume is not found in the docker host.
  81. //
  82. // Deprecated: Use IsErrNotFound
  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. // IsErrNodeNotFound returns true if the error is caused
  101. // when a node is not found.
  102. //
  103. // Deprecated: Use IsErrNotFound
  104. func IsErrNodeNotFound(err error) bool {
  105. return IsErrNotFound(err)
  106. }
  107. // IsErrServiceNotFound returns true if the error is caused
  108. // when a service is not found.
  109. //
  110. // Deprecated: Use IsErrNotFound
  111. func IsErrServiceNotFound(err error) bool {
  112. return IsErrNotFound(err)
  113. }
  114. // IsErrTaskNotFound returns true if the error is caused
  115. // when a task is not found.
  116. //
  117. // Deprecated: Use IsErrNotFound
  118. func IsErrTaskNotFound(err error) bool {
  119. return IsErrNotFound(err)
  120. }
  121. type pluginPermissionDenied struct {
  122. name string
  123. }
  124. func (e pluginPermissionDenied) Error() string {
  125. return "Permission denied while installing plugin " + e.name
  126. }
  127. // IsErrPluginPermissionDenied returns true if the error is caused
  128. // when a user denies a plugin's permissions
  129. func IsErrPluginPermissionDenied(err error) bool {
  130. _, ok := err.(pluginPermissionDenied)
  131. return ok
  132. }
  133. // NewVersionError returns an error if the APIVersion required
  134. // if less than the current supported version
  135. func (cli *Client) NewVersionError(APIrequired, feature string) error {
  136. if cli.version != "" && versions.LessThan(cli.version, APIrequired) {
  137. return fmt.Errorf("%q requires API version %s, but the Docker daemon API version is %s", feature, APIrequired, cli.version)
  138. }
  139. return nil
  140. }
  141. // IsErrSecretNotFound returns true if the error is caused
  142. // when a secret is not found.
  143. //
  144. // Deprecated: Use IsErrNotFound
  145. func IsErrSecretNotFound(err error) bool {
  146. return IsErrNotFound(err)
  147. }
  148. // IsErrConfigNotFound returns true if the error is caused
  149. // when a config is not found.
  150. //
  151. // Deprecated: Use IsErrNotFound
  152. func IsErrConfigNotFound(err error) bool {
  153. return IsErrNotFound(err)
  154. }
  155. // IsErrPluginNotFound returns true if the error is caused
  156. // when a plugin is not found in the docker host.
  157. //
  158. // Deprecated: Use IsErrNotFound
  159. func IsErrPluginNotFound(err error) bool {
  160. return IsErrNotFound(err)
  161. }