hcserror.go 907 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //go:build windows
  2. package hcserror
  3. import (
  4. "errors"
  5. "fmt"
  6. "golang.org/x/sys/windows"
  7. )
  8. type HcsError struct {
  9. title string
  10. rest string
  11. Err error
  12. }
  13. func (e *HcsError) Error() string {
  14. s := e.title
  15. if len(s) > 0 && s[len(s)-1] != ' ' {
  16. s += " "
  17. }
  18. s += fmt.Sprintf("failed in Win32: %s (0x%x)", e.Err, Win32FromError(e.Err))
  19. if e.rest != "" {
  20. if e.rest[0] != ' ' {
  21. s += " "
  22. }
  23. s += e.rest
  24. }
  25. return s
  26. }
  27. func New(err error, title, rest string) error {
  28. // Pass through DLL errors directly since they do not originate from HCS.
  29. var e *windows.DLLError
  30. if errors.As(err, &e) {
  31. return err
  32. }
  33. return &HcsError{title, rest, err}
  34. }
  35. func Win32FromError(err error) uint32 {
  36. var herr *HcsError
  37. if errors.As(err, &herr) {
  38. return Win32FromError(herr.Err)
  39. }
  40. var code windows.Errno
  41. if errors.As(err, &code) {
  42. return uint32(code)
  43. }
  44. return uint32(windows.ERROR_GEN_FAILURE)
  45. }