errors.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package hcsshim
  2. import (
  3. "fmt"
  4. "syscall"
  5. "github.com/Microsoft/hcsshim/internal/hns"
  6. "github.com/Microsoft/hcsshim/internal/hcs"
  7. "github.com/Microsoft/hcsshim/internal/hcserror"
  8. )
  9. var (
  10. // ErrComputeSystemDoesNotExist is an error encountered when the container being operated on no longer exists = hcs.exist
  11. ErrComputeSystemDoesNotExist = hcs.ErrComputeSystemDoesNotExist
  12. // ErrElementNotFound is an error encountered when the object being referenced does not exist
  13. ErrElementNotFound = hcs.ErrElementNotFound
  14. // ErrElementNotFound is an error encountered when the object being referenced does not exist
  15. ErrNotSupported = hcs.ErrNotSupported
  16. // ErrInvalidData is an error encountered when the request being sent to hcs is invalid/unsupported
  17. // decimal -2147024883 / hex 0x8007000d
  18. ErrInvalidData = hcs.ErrInvalidData
  19. // ErrHandleClose is an error encountered when the handle generating the notification being waited on has been closed
  20. ErrHandleClose = hcs.ErrHandleClose
  21. // ErrAlreadyClosed is an error encountered when using a handle that has been closed by the Close method
  22. ErrAlreadyClosed = hcs.ErrAlreadyClosed
  23. // ErrInvalidNotificationType is an error encountered when an invalid notification type is used
  24. ErrInvalidNotificationType = hcs.ErrInvalidNotificationType
  25. // ErrInvalidProcessState is an error encountered when the process is not in a valid state for the requested operation
  26. ErrInvalidProcessState = hcs.ErrInvalidProcessState
  27. // ErrTimeout is an error encountered when waiting on a notification times out
  28. ErrTimeout = hcs.ErrTimeout
  29. // ErrUnexpectedContainerExit is the error encountered when a container exits while waiting for
  30. // a different expected notification
  31. ErrUnexpectedContainerExit = hcs.ErrUnexpectedContainerExit
  32. // ErrUnexpectedProcessAbort is the error encountered when communication with the compute service
  33. // is lost while waiting for a notification
  34. ErrUnexpectedProcessAbort = hcs.ErrUnexpectedProcessAbort
  35. // ErrUnexpectedValue is an error encountered when hcs returns an invalid value
  36. ErrUnexpectedValue = hcs.ErrUnexpectedValue
  37. // ErrVmcomputeAlreadyStopped is an error encountered when a shutdown or terminate request is made on a stopped container
  38. ErrVmcomputeAlreadyStopped = hcs.ErrVmcomputeAlreadyStopped
  39. // ErrVmcomputeOperationPending is an error encountered when the operation is being completed asynchronously
  40. ErrVmcomputeOperationPending = hcs.ErrVmcomputeOperationPending
  41. // ErrVmcomputeOperationInvalidState is an error encountered when the compute system is not in a valid state for the requested operation
  42. ErrVmcomputeOperationInvalidState = hcs.ErrVmcomputeOperationInvalidState
  43. // ErrProcNotFound is an error encountered when the the process cannot be found
  44. ErrProcNotFound = hcs.ErrProcNotFound
  45. // ErrVmcomputeOperationAccessIsDenied is an error which can be encountered when enumerating compute systems in RS1/RS2
  46. // builds when the underlying silo might be in the process of terminating. HCS was fixed in RS3.
  47. ErrVmcomputeOperationAccessIsDenied = hcs.ErrVmcomputeOperationAccessIsDenied
  48. // ErrVmcomputeInvalidJSON is an error encountered when the compute system does not support/understand the messages sent by management
  49. ErrVmcomputeInvalidJSON = hcs.ErrVmcomputeInvalidJSON
  50. // ErrVmcomputeUnknownMessage is an error encountered guest compute system doesn't support the message
  51. ErrVmcomputeUnknownMessage = hcs.ErrVmcomputeUnknownMessage
  52. // ErrNotSupported is an error encountered when hcs doesn't support the request
  53. ErrPlatformNotSupported = hcs.ErrPlatformNotSupported
  54. )
  55. type EndpointNotFoundError = hns.EndpointNotFoundError
  56. type NetworkNotFoundError = hns.NetworkNotFoundError
  57. // ProcessError is an error encountered in HCS during an operation on a Process object
  58. type ProcessError struct {
  59. Process *process
  60. Operation string
  61. Err error
  62. Events []hcs.ErrorEvent
  63. }
  64. // ContainerError is an error encountered in HCS during an operation on a Container object
  65. type ContainerError struct {
  66. Container *container
  67. Operation string
  68. Err error
  69. Events []hcs.ErrorEvent
  70. }
  71. func (e *ContainerError) Error() string {
  72. if e == nil {
  73. return "<nil>"
  74. }
  75. if e.Container == nil {
  76. return "unexpected nil container for error: " + e.Err.Error()
  77. }
  78. s := "container " + e.Container.system.ID()
  79. if e.Operation != "" {
  80. s += " encountered an error during " + e.Operation
  81. }
  82. switch e.Err.(type) {
  83. case nil:
  84. break
  85. case syscall.Errno:
  86. s += fmt.Sprintf(": failure in a Windows system call: %s (0x%x)", e.Err, hcserror.Win32FromError(e.Err))
  87. default:
  88. s += fmt.Sprintf(": %s", e.Err.Error())
  89. }
  90. for _, ev := range e.Events {
  91. s += "\n" + ev.String()
  92. }
  93. return s
  94. }
  95. func (e *ProcessError) Error() string {
  96. if e == nil {
  97. return "<nil>"
  98. }
  99. if e.Process == nil {
  100. return "Unexpected nil process for error: " + e.Err.Error()
  101. }
  102. s := fmt.Sprintf("process %d in container %s", e.Process.p.Pid(), e.Process.p.SystemID())
  103. if e.Operation != "" {
  104. s += " encountered an error during " + e.Operation
  105. }
  106. switch e.Err.(type) {
  107. case nil:
  108. break
  109. case syscall.Errno:
  110. s += fmt.Sprintf(": failure in a Windows system call: %s (0x%x)", e.Err, hcserror.Win32FromError(e.Err))
  111. default:
  112. s += fmt.Sprintf(": %s", e.Err.Error())
  113. }
  114. for _, ev := range e.Events {
  115. s += "\n" + ev.String()
  116. }
  117. return s
  118. }
  119. // IsNotExist checks if an error is caused by the Container or Process not existing.
  120. // Note: Currently, ErrElementNotFound can mean that a Process has either
  121. // already exited, or does not exist. Both IsAlreadyStopped and IsNotExist
  122. // will currently return true when the error is ErrElementNotFound or ErrProcNotFound.
  123. func IsNotExist(err error) bool {
  124. if _, ok := err.(EndpointNotFoundError); ok {
  125. return true
  126. }
  127. if _, ok := err.(NetworkNotFoundError); ok {
  128. return true
  129. }
  130. return hcs.IsNotExist(getInnerError(err))
  131. }
  132. // IsAlreadyClosed checks if an error is caused by the Container or Process having been
  133. // already closed by a call to the Close() method.
  134. func IsAlreadyClosed(err error) bool {
  135. return hcs.IsAlreadyClosed(getInnerError(err))
  136. }
  137. // IsPending returns a boolean indicating whether the error is that
  138. // the requested operation is being completed in the background.
  139. func IsPending(err error) bool {
  140. return hcs.IsPending(getInnerError(err))
  141. }
  142. // IsTimeout returns a boolean indicating whether the error is caused by
  143. // a timeout waiting for the operation to complete.
  144. func IsTimeout(err error) bool {
  145. return hcs.IsTimeout(getInnerError(err))
  146. }
  147. // IsAlreadyStopped returns a boolean indicating whether the error is caused by
  148. // a Container or Process being already stopped.
  149. // Note: Currently, ErrElementNotFound can mean that a Process has either
  150. // already exited, or does not exist. Both IsAlreadyStopped and IsNotExist
  151. // will currently return true when the error is ErrElementNotFound or ErrProcNotFound.
  152. func IsAlreadyStopped(err error) bool {
  153. return hcs.IsAlreadyStopped(getInnerError(err))
  154. }
  155. // IsNotSupported returns a boolean indicating whether the error is caused by
  156. // unsupported platform requests
  157. // Note: Currently Unsupported platform requests can be mean either
  158. // ErrVmcomputeInvalidJSON, ErrInvalidData, ErrNotSupported or ErrVmcomputeUnknownMessage
  159. // is thrown from the Platform
  160. func IsNotSupported(err error) bool {
  161. return hcs.IsNotSupported(getInnerError(err))
  162. }
  163. // IsOperationInvalidState returns true when err is caused by
  164. // `ErrVmcomputeOperationInvalidState`.
  165. func IsOperationInvalidState(err error) bool {
  166. return hcs.IsOperationInvalidState(getInnerError(err))
  167. }
  168. // IsAccessIsDenied returns true when err is caused by
  169. // `ErrVmcomputeOperationAccessIsDenied`.
  170. func IsAccessIsDenied(err error) bool {
  171. return hcs.IsAccessIsDenied(getInnerError(err))
  172. }
  173. func getInnerError(err error) error {
  174. switch pe := err.(type) {
  175. case nil:
  176. return nil
  177. case *ContainerError:
  178. err = pe.Err
  179. case *ProcessError:
  180. err = pe.Err
  181. }
  182. return err
  183. }
  184. func convertSystemError(err error, c *container) error {
  185. if serr, ok := err.(*hcs.SystemError); ok {
  186. return &ContainerError{Container: c, Operation: serr.Op, Err: serr.Err, Events: serr.Events}
  187. }
  188. return err
  189. }
  190. func convertProcessError(err error, p *process) error {
  191. if perr, ok := err.(*hcs.ProcessError); ok {
  192. return &ProcessError{Process: p, Operation: perr.Op, Err: perr.Err, Events: perr.Events}
  193. }
  194. return err
  195. }