errno.go

 1package sys
 2
 3import "strconv"
 4
 5// Errno is a subset of POSIX errno used by wazero interfaces. Zero is not an
 6// error. Other values should not be interpreted numerically, rather by constants
 7// prefixed with 'E'.
 8//
 9// See https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html
10type Errno uint16
11
12// ^-- Note: This will eventually move to the public /sys package. It is
13// experimental until we audit the socket related APIs to ensure we have all
14// the Errno it returns, and we export fs.FS. This is not in /internal/sys as
15// that would introduce a package cycle.
16
17// This is a subset of errors to reduce implementation burden. `wasip1` defines
18// almost all POSIX error numbers, but not all are used in practice. wazero
19// will add ones needed in POSIX order, as needed by functions that explicitly
20// document returning them.
21//
22// https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-errno-enumu16
23const (
24	EACCES Errno = iota + 1
25	EAGAIN
26	EBADF
27	EEXIST
28	EFAULT
29	EINTR
30	EINVAL
31	EIO
32	EISDIR
33	ELOOP
34	ENAMETOOLONG
35	ENOENT
36	ENOSYS
37	ENOTDIR
38	ERANGE
39	ENOTEMPTY
40	ENOTSOCK
41	ENOTSUP
42	EPERM
43	EROFS
44
45	// NOTE ENOTCAPABLE is defined in wasip1, but not in POSIX. wasi-libc
46	// converts it to EBADF, ESPIPE or EINVAL depending on the call site.
47	// It isn't known if compilers who don't use ENOTCAPABLE would crash on it.
48)
49
50// Error implements error
51func (e Errno) Error() string {
52	switch e {
53	case 0: // not an error
54		return "success"
55	case EACCES:
56		return "permission denied"
57	case EAGAIN:
58		return "resource unavailable, try again"
59	case EBADF:
60		return "bad file descriptor"
61	case EEXIST:
62		return "file exists"
63	case EFAULT:
64		return "bad address"
65	case EINTR:
66		return "interrupted function"
67	case EINVAL:
68		return "invalid argument"
69	case EIO:
70		return "input/output error"
71	case EISDIR:
72		return "is a directory"
73	case ELOOP:
74		return "too many levels of symbolic links"
75	case ENAMETOOLONG:
76		return "filename too long"
77	case ENOENT:
78		return "no such file or directory"
79	case ENOSYS:
80		return "functionality not supported"
81	case ENOTDIR:
82		return "not a directory or a symbolic link to a directory"
83	case ERANGE:
84		return "result too large"
85	case ENOTEMPTY:
86		return "directory not empty"
87	case ENOTSOCK:
88		return "not a socket"
89	case ENOTSUP:
90		return "not supported (may be the same value as [EOPNOTSUPP])"
91	case EPERM:
92		return "operation not permitted"
93	case EROFS:
94		return "read-only file system"
95	default:
96		return "Errno(" + strconv.Itoa(int(e)) + ")"
97	}
98}