Skip to content

Commit 3680d87

Browse files
tetragon/windows: Port package errmetrics to Windows
Add windows specific definition of GetErrorMessage() function to convert error code into string, using the sys/Windows package. Added unit test to check strings related to common Windows errors. Signed-off-by: Anadi Anadi <[email protected]>
1 parent c550d9f commit 3680d87

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

pkg/errmetrics/err_msg_windows.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package errmetrics
2+
3+
import (
4+
"syscall"
5+
6+
"golang.org/x/sys/windows"
7+
)
8+
9+
func GetErrorMessage(err uint16) (message string) {
10+
const flags uint32 = syscall.FORMAT_MESSAGE_FROM_SYSTEM
11+
buf := make([]uint16, 300)
12+
_, error := windows.FormatMessage(flags, 0, uint32(err), 0, buf, nil)
13+
if error != nil {
14+
return "unknown error code "
15+
}
16+
return windows.UTF16ToString(buf[:])
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package errmetrics
2+
3+
import (
4+
"strings"
5+
"syscall"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestErrMessage(t *testing.T) {
12+
s1 := GetErrorMessage(uint16(syscall.ERROR_ACCESS_DENIED))
13+
assert.Equal(t, strings.HasPrefix(s1, "Access is denied."), true)
14+
15+
s2 := GetErrorMessage(uint16(syscall.ERROR_INSUFFICIENT_BUFFER))
16+
assert.Equal(t, strings.HasPrefix(s2, "The data area passed to a system call is too small."), true)
17+
18+
}

0 commit comments

Comments
 (0)