github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/experimental/sys/error.go (about) 1 package sys 2 3 import ( 4 "io" 5 "io/fs" 6 "os" 7 ) 8 9 // UnwrapOSError returns an Errno or zero if the input is nil. 10 func UnwrapOSError(err error) Errno { 11 if err == nil { 12 return 0 13 } 14 err = underlyingError(err) 15 switch err { 16 case nil, io.EOF: 17 return 0 // EOF is not a Errno 18 case fs.ErrInvalid: 19 return EINVAL 20 case fs.ErrPermission: 21 return EPERM 22 case fs.ErrExist: 23 return EEXIST 24 case fs.ErrNotExist: 25 return ENOENT 26 case fs.ErrClosed: 27 return EBADF 28 } 29 return errorToErrno(err) 30 } 31 32 // underlyingError returns the underlying error if a well-known OS error type. 33 // 34 // This impl is basically the same as os.underlyingError in os/error.go 35 func underlyingError(err error) error { 36 switch err := err.(type) { 37 case *os.PathError: 38 return err.Err 39 case *os.LinkError: 40 return err.Err 41 case *os.SyscallError: 42 return err.Err 43 } 44 return err 45 }