errors.go 8.2 KB

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