UNPKG

@zenfs/core

Version:

A filesystem, anywhere

290 lines (289 loc) 11 kB
/** * Standard libc error codes. More will be added to this enum and error strings as they are * needed. * @see https://en.wikipedia.org/wiki/Errno.h */ export var Errno; (function (Errno) { /** Operation not permitted */ Errno[Errno["EPERM"] = 1] = "EPERM"; /** No such file or directory */ Errno[Errno["ENOENT"] = 2] = "ENOENT"; /** Interrupted system call */ Errno[Errno["EINTR"] = 4] = "EINTR"; /** Input/output error */ Errno[Errno["EIO"] = 5] = "EIO"; /** No such device or address */ Errno[Errno["ENXIO"] = 6] = "ENXIO"; /** Bad file descriptor */ Errno[Errno["EBADF"] = 9] = "EBADF"; /** Resource temporarily unavailable */ Errno[Errno["EAGAIN"] = 11] = "EAGAIN"; /** Cannot allocate memory */ Errno[Errno["ENOMEM"] = 12] = "ENOMEM"; /** Permission denied */ Errno[Errno["EACCES"] = 13] = "EACCES"; /** Bad address */ Errno[Errno["EFAULT"] = 14] = "EFAULT"; /** Block device required */ Errno[Errno["ENOTBLK"] = 15] = "ENOTBLK"; /** Resource busy or locked */ Errno[Errno["EBUSY"] = 16] = "EBUSY"; /** File exists */ Errno[Errno["EEXIST"] = 17] = "EEXIST"; /** Invalid cross-device link */ Errno[Errno["EXDEV"] = 18] = "EXDEV"; /** No such device */ Errno[Errno["ENODEV"] = 19] = "ENODEV"; /** File is not a directory */ Errno[Errno["ENOTDIR"] = 20] = "ENOTDIR"; /** File is a directory */ Errno[Errno["EISDIR"] = 21] = "EISDIR"; /** Invalid argument */ Errno[Errno["EINVAL"] = 22] = "EINVAL"; /** Too many open files in system */ Errno[Errno["ENFILE"] = 23] = "ENFILE"; /** Too many open files */ Errno[Errno["EMFILE"] = 24] = "EMFILE"; /** Text file busy */ Errno[Errno["ETXTBSY"] = 26] = "ETXTBSY"; /** File is too big */ Errno[Errno["EFBIG"] = 27] = "EFBIG"; /** No space left on disk */ Errno[Errno["ENOSPC"] = 28] = "ENOSPC"; /** Illegal seek */ Errno[Errno["ESPIPE"] = 29] = "ESPIPE"; /** Cannot modify a read-only file system */ Errno[Errno["EROFS"] = 30] = "EROFS"; /** Too many links */ Errno[Errno["EMLINK"] = 31] = "EMLINK"; /** Broken pipe */ Errno[Errno["EPIPE"] = 32] = "EPIPE"; /** Numerical argument out of domain */ Errno[Errno["EDOM"] = 33] = "EDOM"; /** Numerical result out of range */ Errno[Errno["ERANGE"] = 34] = "ERANGE"; /** Resource deadlock would occur */ Errno[Errno["EDEADLK"] = 35] = "EDEADLK"; /** File name too long */ Errno[Errno["ENAMETOOLONG"] = 36] = "ENAMETOOLONG"; /** No locks available */ Errno[Errno["ENOLCK"] = 37] = "ENOLCK"; /** Function not implemented */ Errno[Errno["ENOSYS"] = 38] = "ENOSYS"; /** Directory is not empty */ Errno[Errno["ENOTEMPTY"] = 39] = "ENOTEMPTY"; /** Too many levels of symbolic links */ Errno[Errno["ELOOP"] = 40] = "ELOOP"; /** No message of desired type */ Errno[Errno["ENOMSG"] = 42] = "ENOMSG"; /** Invalid exchange */ Errno[Errno["EBADE"] = 52] = "EBADE"; /** Invalid request descriptor */ Errno[Errno["EBADR"] = 53] = "EBADR"; /** Exchange full */ Errno[Errno["EXFULL"] = 54] = "EXFULL"; /** No anode */ Errno[Errno["ENOANO"] = 55] = "ENOANO"; /** Invalid request code */ Errno[Errno["EBADRQC"] = 56] = "EBADRQC"; /** Device not a stream */ Errno[Errno["ENOSTR"] = 60] = "ENOSTR"; /** No data available */ Errno[Errno["ENODATA"] = 61] = "ENODATA"; /** Timer expired */ Errno[Errno["ETIME"] = 62] = "ETIME"; /** Out of streams resources */ Errno[Errno["ENOSR"] = 63] = "ENOSR"; /** Machine is not on the network */ Errno[Errno["ENONET"] = 64] = "ENONET"; /** Object is remote */ Errno[Errno["EREMOTE"] = 66] = "EREMOTE"; /** Link has been severed */ Errno[Errno["ENOLINK"] = 67] = "ENOLINK"; /** Communication error on send */ Errno[Errno["ECOMM"] = 70] = "ECOMM"; /** Protocol error */ Errno[Errno["EPROTO"] = 71] = "EPROTO"; /** Bad message */ Errno[Errno["EBADMSG"] = 74] = "EBADMSG"; /** Value too large for defined data type */ Errno[Errno["EOVERFLOW"] = 75] = "EOVERFLOW"; /** File descriptor in bad state */ Errno[Errno["EBADFD"] = 77] = "EBADFD"; /** Streams pipe error */ Errno[Errno["ESTRPIPE"] = 86] = "ESTRPIPE"; /** Socket operation on non-socket */ Errno[Errno["ENOTSOCK"] = 88] = "ENOTSOCK"; /** Destination address required */ Errno[Errno["EDESTADDRREQ"] = 89] = "EDESTADDRREQ"; /** Message too long */ Errno[Errno["EMSGSIZE"] = 90] = "EMSGSIZE"; /** Protocol wrong type for socket */ Errno[Errno["EPROTOTYPE"] = 91] = "EPROTOTYPE"; /** Protocol not available */ Errno[Errno["ENOPROTOOPT"] = 92] = "ENOPROTOOPT"; /** Protocol not supported */ Errno[Errno["EPROTONOSUPPORT"] = 93] = "EPROTONOSUPPORT"; /** Socket type not supported */ Errno[Errno["ESOCKTNOSUPPORT"] = 94] = "ESOCKTNOSUPPORT"; /** Operation is not supported */ Errno[Errno["ENOTSUP"] = 95] = "ENOTSUP"; /** Network is down */ Errno[Errno["ENETDOWN"] = 100] = "ENETDOWN"; /** Network is unreachable */ Errno[Errno["ENETUNREACH"] = 101] = "ENETUNREACH"; /** Network dropped connection on reset */ Errno[Errno["ENETRESET"] = 102] = "ENETRESET"; /** Connection timed out */ Errno[Errno["ETIMEDOUT"] = 110] = "ETIMEDOUT"; /** Connection refused */ Errno[Errno["ECONNREFUSED"] = 111] = "ECONNREFUSED"; /** Host is down */ Errno[Errno["EHOSTDOWN"] = 112] = "EHOSTDOWN"; /** No route to host */ Errno[Errno["EHOSTUNREACH"] = 113] = "EHOSTUNREACH"; /** Operation already in progress */ Errno[Errno["EALREADY"] = 114] = "EALREADY"; /** Operation now in progress */ Errno[Errno["EINPROGRESS"] = 115] = "EINPROGRESS"; /** Stale file handle */ Errno[Errno["ESTALE"] = 116] = "ESTALE"; /** Remote I/O error */ Errno[Errno["EREMOTEIO"] = 121] = "EREMOTEIO"; /** Disk quota exceeded */ Errno[Errno["EDQUOT"] = 122] = "EDQUOT"; })(Errno || (Errno = {})); /** * Strings associated with each error code. * @internal */ export const errorMessages = { [Errno.EPERM]: 'Operation not permitted', [Errno.ENOENT]: 'No such file or directory', [Errno.EINTR]: 'Interrupted system call', [Errno.EIO]: 'Input/output error', [Errno.ENXIO]: 'No such device or address', [Errno.EBADF]: 'Bad file descriptor', [Errno.EAGAIN]: 'Resource temporarily unavailable', [Errno.ENOMEM]: 'Cannot allocate memory', [Errno.EACCES]: 'Permission denied', [Errno.EFAULT]: 'Bad address', [Errno.ENOTBLK]: 'Block device required', [Errno.EBUSY]: 'Resource busy or locked', [Errno.EEXIST]: 'File exists', [Errno.EXDEV]: 'Invalid cross-device link', [Errno.ENODEV]: 'No such device', [Errno.ENOTDIR]: 'File is not a directory', [Errno.EISDIR]: 'File is a directory', [Errno.EINVAL]: 'Invalid argument', [Errno.ENFILE]: 'Too many open files in system', [Errno.EMFILE]: 'Too many open files', [Errno.ETXTBSY]: 'Text file busy', [Errno.EFBIG]: 'File is too big', [Errno.ENOSPC]: 'No space left on disk', [Errno.ESPIPE]: 'Illegal seek', [Errno.EROFS]: 'Cannot modify a read-only file system', [Errno.EMLINK]: 'Too many links', [Errno.EPIPE]: 'Broken pipe', [Errno.EDOM]: 'Numerical argument out of domain', [Errno.ERANGE]: 'Numerical result out of range', [Errno.EDEADLK]: 'Resource deadlock would occur', [Errno.ENAMETOOLONG]: 'File name too long', [Errno.ENOLCK]: 'No locks available', [Errno.ENOSYS]: 'Function not implemented', [Errno.ENOTEMPTY]: 'Directory is not empty', [Errno.ELOOP]: 'Too many levels of symbolic links', [Errno.ENOMSG]: 'No message of desired type', [Errno.EBADE]: 'Invalid exchange', [Errno.EBADR]: 'Invalid request descriptor', [Errno.EXFULL]: 'Exchange full', [Errno.ENOANO]: 'No anode', [Errno.EBADRQC]: 'Invalid request code', [Errno.ENOSTR]: 'Device not a stream', [Errno.ENODATA]: 'No data available', [Errno.ETIME]: 'Timer expired', [Errno.ENOSR]: 'Out of streams resources', [Errno.ENONET]: 'Machine is not on the network', [Errno.EREMOTE]: 'Object is remote', [Errno.ENOLINK]: 'Link has been severed', [Errno.ECOMM]: 'Communication error on send', [Errno.EPROTO]: 'Protocol error', [Errno.EBADMSG]: 'Bad message', [Errno.EOVERFLOW]: 'Value too large for defined data type', [Errno.EBADFD]: 'File descriptor in bad state', [Errno.ESTRPIPE]: 'Streams pipe error', [Errno.ENOTSOCK]: 'Socket operation on non-socket', [Errno.EDESTADDRREQ]: 'Destination address required', [Errno.EMSGSIZE]: 'Message too long', [Errno.EPROTOTYPE]: 'Protocol wrong type for socket', [Errno.ENOPROTOOPT]: 'Protocol not available', [Errno.EPROTONOSUPPORT]: 'Protocol not supported', [Errno.ESOCKTNOSUPPORT]: 'Socket type not supported', [Errno.ENOTSUP]: 'Operation is not supported', [Errno.ENETDOWN]: 'Network is down', [Errno.ENETUNREACH]: 'Network is unreachable', [Errno.ENETRESET]: 'Network dropped connection on reset', [Errno.ETIMEDOUT]: 'Connection timed out', [Errno.ECONNREFUSED]: 'Connection refused', [Errno.EHOSTDOWN]: 'Host is down', [Errno.EHOSTUNREACH]: 'No route to host', [Errno.EALREADY]: 'Operation already in progress', [Errno.EINPROGRESS]: 'Operation now in progress', [Errno.ESTALE]: 'Stale file handle', [Errno.EREMOTEIO]: 'Remote I/O error', [Errno.EDQUOT]: 'Disk quota exceeded', }; /** * An error with additional information about what happened */ export class ErrnoError extends Error { static fromJSON(json) { const err = new ErrnoError(json.errno, json.message, json.path, json.syscall); err.code = json.code; err.stack = json.stack; return err; } static With(code, path, syscall) { return new ErrnoError(Errno[code], errorMessages[Errno[code]], path, syscall); } constructor( /** * The kind of error */ errno, /** * A descriptive error message */ message = errorMessages[errno], path, syscall = '') { super(message); this.errno = errno; this.path = path; this.syscall = syscall; this.code = Errno[errno]; this.message = this.code + ': ' + message + (this.path ? `, '${this.path}'` : ''); } /** * @returns A friendly error message. */ toString() { return this.message; } toJSON() { return { errno: this.errno, code: this.code, path: this.path, stack: this.stack, message: this.message, syscall: this.syscall, }; } /** * The size of the API error in buffer-form in bytes. */ bufferSize() { // 4 bytes for string length. return 4 + JSON.stringify(this.toJSON()).length; } }