errors.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package client // import "github.com/docker/docker/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. // unauthorizedError represents an authorization error in a remote registry.
  61. type unauthorizedError struct {
  62. cause error
  63. }
  64. // Error returns a string representation of an unauthorizedError
  65. func (u unauthorizedError) Error() string {
  66. return u.cause.Error()
  67. }
  68. // IsErrUnauthorized returns true if the error is caused
  69. // when a remote registry authentication fails
  70. func IsErrUnauthorized(err error) bool {
  71. _, ok := err.(unauthorizedError)
  72. return ok
  73. }
  74. type pluginPermissionDenied struct {
  75. name string
  76. }
  77. func (e pluginPermissionDenied) Error() string {
  78. return "Permission denied while installing plugin " + e.name
  79. }
  80. // IsErrPluginPermissionDenied returns true if the error is caused
  81. // when a user denies a plugin's permissions
  82. func IsErrPluginPermissionDenied(err error) bool {
  83. _, ok := err.(pluginPermissionDenied)
  84. return ok
  85. }
  86. type notImplementedError struct {
  87. message string
  88. }
  89. func (e notImplementedError) Error() string {
  90. return e.message
  91. }
  92. func (e notImplementedError) NotImplemented() bool {
  93. return true
  94. }
  95. // IsErrNotImplemented returns true if the error is a NotImplemented error.
  96. // This is returned by the API when a requested feature has not been
  97. // implemented.
  98. func IsErrNotImplemented(err error) bool {
  99. te, ok := err.(notImplementedError)
  100. return ok && te.NotImplemented()
  101. }
  102. // NewVersionError returns an error if the APIVersion required
  103. // if less than the current supported version
  104. func (cli *Client) NewVersionError(APIrequired, feature string) error {
  105. if cli.version != "" && versions.LessThan(cli.version, APIrequired) {
  106. return fmt.Errorf("%q requires API version %s, but the Docker daemon API version is %s", feature, APIrequired, cli.version)
  107. }
  108. return nil
  109. }