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
|
package linux
import (
"errors"
"os"
)
// ExtractErrno extracts an [Errno] from an error, best effort.
//
// If the system-specific or Go-specific error cannot be mapped to anything, it
// will be logged and EIO will be returned.
func ExtractErrno(err error) Errno {
for _, pair := range []struct {
error
Errno
}{
{os.ErrNotExist, ENOENT},
{os.ErrExist, EEXIST},
{os.ErrPermission, EACCES},
{os.ErrInvalid, EINVAL},
} {
if errors.Is(err, pair.error) {
return pair.Errno
}
}
var errno Errno
if errors.As(err, &errno) {
return errno
}
if e := sysErrno(err); e != 0 {
return e
}
// Default case.
return EIO
}
|