github.com/tetratelabs/wazero@v1.2.1/internal/gojs/errno.go (about)

     1  package gojs
     2  
     3  import (
     4  	"io"
     5  	"syscall"
     6  
     7  	"github.com/tetratelabs/wazero/internal/platform"
     8  )
     9  
    10  // Errno is a (GOARCH=wasm) error, which must match a key in mapJSError.
    11  //
    12  // See https://github.com/golang/go/blob/go1.20/src/syscall/tables_js.go#L371-L494
    13  type Errno struct {
    14  	s string
    15  }
    16  
    17  // Error implements error.
    18  func (e *Errno) Error() string {
    19  	return e.s
    20  }
    21  
    22  // This order match constants from wasi_snapshot_preview1.ErrnoSuccess for
    23  // easier maintenance.
    24  var (
    25  	// ErrnoAcces Permission denied.
    26  	ErrnoAcces = &Errno{"EACCES"}
    27  	// ErrnoAgain Resource unavailable, or operation would block.
    28  	ErrnoAgain = &Errno{"EAGAIN"}
    29  	// ErrnoBadf Bad file descriptor.
    30  	ErrnoBadf = &Errno{"EBADF"}
    31  	// ErrnoExist File exists.
    32  	ErrnoExist = &Errno{"EEXIST"}
    33  	// ErrnoFault Bad address.
    34  	ErrnoFault = &Errno{"EFAULT"}
    35  	// ErrnoIntr Interrupted function.
    36  	ErrnoIntr = &Errno{"EINTR"}
    37  	// ErrnoInval Invalid argument.
    38  	ErrnoInval = &Errno{"EINVAL"}
    39  	// ErrnoIo I/O error.
    40  	ErrnoIo = &Errno{"EIO"}
    41  	// ErrnoIsdir Is a directory.
    42  	ErrnoIsdir = &Errno{"EISDIR"}
    43  	// ErrnoLoop Too many levels of symbolic links.
    44  	ErrnoLoop = &Errno{"ELOOP"}
    45  	// ErrnoNametoolong Filename too long.
    46  	ErrnoNametoolong = &Errno{"ENAMETOOLONG"}
    47  	// ErrnoNoent No such file or directory.
    48  	ErrnoNoent = &Errno{"ENOENT"}
    49  	// ErrnoNosys function not supported.
    50  	ErrnoNosys = &Errno{"ENOSYS"}
    51  	// ErrnoNotdir Not a directory or a symbolic link to a directory.
    52  	ErrnoNotdir = &Errno{"ENOTDIR"}
    53  	// ErrnoNotempty Directory not empty.
    54  	ErrnoNotempty = &Errno{"ENOTEMPTY"}
    55  	// ErrnoNotsup Not supported, or operation not supported on socket.
    56  	ErrnoNotsup = &Errno{"ENOTSUP"}
    57  	// ErrnoPerm Operation not permitted.
    58  	ErrnoPerm = &Errno{"EPERM"}
    59  	// ErrnoRofs read-only file system.
    60  	ErrnoRofs = &Errno{"EROFS"}
    61  )
    62  
    63  // ToErrno maps I/O errors as the message must be the code, ex. "EINVAL", not
    64  // the message, e.g. "invalid argument".
    65  //
    66  // This should match wasi_snapshot_preview1.ToErrno for maintenance ease.
    67  func ToErrno(err error) *Errno {
    68  	if err == nil || err == io.EOF {
    69  		return nil // io.EOF has no value in GOOS=js, and isn't an error.
    70  	}
    71  	errno := platform.UnwrapOSError(err)
    72  
    73  	switch errno {
    74  	case syscall.EACCES:
    75  		return ErrnoAcces
    76  	case syscall.EAGAIN:
    77  		return ErrnoAgain
    78  	case syscall.EBADF:
    79  		return ErrnoBadf
    80  	case syscall.EEXIST:
    81  		return ErrnoExist
    82  	case syscall.EFAULT:
    83  		return ErrnoFault
    84  	case syscall.EINTR:
    85  		return ErrnoIntr
    86  	case syscall.EINVAL:
    87  		return ErrnoInval
    88  	case syscall.EIO:
    89  		return ErrnoIo
    90  	case syscall.EISDIR:
    91  		return ErrnoIsdir
    92  	case syscall.ELOOP:
    93  		return ErrnoLoop
    94  	case syscall.ENAMETOOLONG:
    95  		return ErrnoNametoolong
    96  	case syscall.ENOENT:
    97  		return ErrnoNoent
    98  	case syscall.ENOSYS:
    99  		return ErrnoNosys
   100  	case syscall.ENOTDIR:
   101  		return ErrnoNotdir
   102  	case syscall.ENOTEMPTY:
   103  		return ErrnoNotempty
   104  	case syscall.ENOTSUP:
   105  		return ErrnoNotsup
   106  	case syscall.EPERM:
   107  		return ErrnoPerm
   108  	case syscall.EROFS:
   109  		return ErrnoRofs
   110  	default:
   111  		return ErrnoIo
   112  	}
   113  }