summaryrefslogtreecommitdiff
path: root/vendor/github.com/Microsoft/hcsshim/internal/hcserror/hcserror.go
blob: c8d362c66cba4d722f9fb80585c11cbd8f9ed562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package hcserror

import (
	"fmt"
	"syscall"
)

const ERROR_GEN_FAILURE = syscall.Errno(31)

type HcsError struct {
	title string
	rest  string
	Err   error
}

func (e *HcsError) Error() string {
	s := e.title
	if len(s) > 0 && s[len(s)-1] != ' ' {
		s += " "
	}
	s += fmt.Sprintf("failed in Win32: %s (0x%x)", e.Err, Win32FromError(e.Err))
	if e.rest != "" {
		if e.rest[0] != ' ' {
			s += " "
		}
		s += e.rest
	}
	return s
}

func New(err error, title, rest string) error {
	// Pass through DLL errors directly since they do not originate from HCS.
	if _, ok := err.(*syscall.DLLError); ok {
		return err
	}
	return &HcsError{title, rest, err}
}

func Errorf(err error, title, format string, a ...interface{}) error {
	return New(err, title, fmt.Sprintf(format, a...))
}

func Win32FromError(err error) uint32 {
	if herr, ok := err.(*HcsError); ok {
		return Win32FromError(herr.Err)
	}
	if code, ok := err.(syscall.Errno); ok {
		return uint32(code)
	}
	return uint32(ERROR_GEN_FAILURE)
}