errors.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "fmt"
  4. "strings"
  5. "syscall"
  6. "github.com/docker/docker/errdefs"
  7. "github.com/pkg/errors"
  8. "google.golang.org/grpc/status"
  9. )
  10. func errNotRunning(id string) error {
  11. return errdefs.Conflict(errors.Errorf("Container %s is not running", id))
  12. }
  13. func containerNotFound(id string) error {
  14. return objNotFoundError{"container", id}
  15. }
  16. type objNotFoundError struct {
  17. object string
  18. id string
  19. }
  20. func (e objNotFoundError) Error() string {
  21. return "No such " + e.object + ": " + e.id
  22. }
  23. func (e objNotFoundError) NotFound() {}
  24. func errContainerIsRestarting(containerID string) error {
  25. cause := errors.Errorf("Container %s is restarting, wait until the container is running", containerID)
  26. return errdefs.Conflict(cause)
  27. }
  28. func errExecNotFound(id string) error {
  29. return objNotFoundError{"exec instance", id}
  30. }
  31. func errExecPaused(id string) error {
  32. cause := errors.Errorf("Container %s is paused, unpause the container before exec", id)
  33. return errdefs.Conflict(cause)
  34. }
  35. func errNotPaused(id string) error {
  36. cause := errors.Errorf("Container %s is already paused", id)
  37. return errdefs.Conflict(cause)
  38. }
  39. type nameConflictError struct {
  40. id string
  41. name string
  42. }
  43. func (e nameConflictError) Error() string {
  44. return fmt.Sprintf("Conflict. The container name %q is already in use by container %q. You have to remove (or rename) that container to be able to reuse that name.", e.name, e.id)
  45. }
  46. func (nameConflictError) Conflict() {}
  47. type containerNotModifiedError struct {
  48. running bool
  49. }
  50. func (e containerNotModifiedError) Error() string {
  51. if e.running {
  52. return "Container is already started"
  53. }
  54. return "Container is already stopped"
  55. }
  56. func (e containerNotModifiedError) NotModified() {}
  57. type invalidIdentifier string
  58. func (e invalidIdentifier) Error() string {
  59. return fmt.Sprintf("invalid name or ID supplied: %q", string(e))
  60. }
  61. func (invalidIdentifier) InvalidParameter() {}
  62. type duplicateMountPointError string
  63. func (e duplicateMountPointError) Error() string {
  64. return "Duplicate mount point: " + string(e)
  65. }
  66. func (duplicateMountPointError) InvalidParameter() {}
  67. type containerFileNotFound struct {
  68. file string
  69. container string
  70. }
  71. func (e containerFileNotFound) Error() string {
  72. return "Could not find the file " + e.file + " in container " + e.container
  73. }
  74. func (containerFileNotFound) NotFound() {}
  75. type invalidFilter struct {
  76. filter string
  77. value interface{}
  78. }
  79. func (e invalidFilter) Error() string {
  80. msg := "Invalid filter '" + e.filter
  81. if e.value != nil {
  82. msg += fmt.Sprintf("=%s", e.value)
  83. }
  84. return msg + "'"
  85. }
  86. func (e invalidFilter) InvalidParameter() {}
  87. type startInvalidConfigError string
  88. func (e startInvalidConfigError) Error() string {
  89. return string(e)
  90. }
  91. func (e startInvalidConfigError) InvalidParameter() {} // Is this right???
  92. func translateContainerdStartErr(cmd string, setExitCode func(int), err error) error {
  93. errDesc := status.Convert(err).Message()
  94. contains := func(s1, s2 string) bool {
  95. return strings.Contains(strings.ToLower(s1), s2)
  96. }
  97. var retErr = errdefs.Unknown(errors.New(errDesc))
  98. // if we receive an internal error from the initial start of a container then lets
  99. // return it instead of entering the restart loop
  100. // set to 127 for container cmd not found/does not exist)
  101. if contains(errDesc, cmd) &&
  102. (contains(errDesc, "executable file not found") ||
  103. contains(errDesc, "no such file or directory") ||
  104. contains(errDesc, "system cannot find the file specified")) {
  105. setExitCode(127)
  106. retErr = startInvalidConfigError(errDesc)
  107. }
  108. // set to 126 for container cmd can't be invoked errors
  109. if contains(errDesc, syscall.EACCES.Error()) {
  110. setExitCode(126)
  111. retErr = startInvalidConfigError(errDesc)
  112. }
  113. // attempted to mount a file onto a directory, or a directory onto a file, maybe from user specified bind mounts
  114. if contains(errDesc, syscall.ENOTDIR.Error()) {
  115. errDesc += ": Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type"
  116. setExitCode(127)
  117. retErr = startInvalidConfigError(errDesc)
  118. }
  119. // TODO: it would be nice to get some better errors from containerd so we can return better errors here
  120. return retErr
  121. }