@zenfs/core
Version:
A filesystem, anywhere
290 lines (289 loc) • 11 kB
JavaScript
/**
* 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 = {
[]: 'Operation not permitted',
[]: 'No such file or directory',
[]: 'Interrupted system call',
[]: 'Input/output error',
[]: 'No such device or address',
[]: 'Bad file descriptor',
[]: 'Resource temporarily unavailable',
[]: 'Cannot allocate memory',
[]: 'Permission denied',
[]: 'Bad address',
[]: 'Block device required',
[]: 'Resource busy or locked',
[]: 'File exists',
[]: 'Invalid cross-device link',
[]: 'No such device',
[]: 'File is not a directory',
[]: 'File is a directory',
[]: 'Invalid argument',
[]: 'Too many open files in system',
[]: 'Too many open files',
[]: 'Text file busy',
[]: 'File is too big',
[]: 'No space left on disk',
[]: 'Illegal seek',
[]: 'Cannot modify a read-only file system',
[]: 'Too many links',
[]: 'Broken pipe',
[]: 'Numerical argument out of domain',
[]: 'Numerical result out of range',
[]: 'Resource deadlock would occur',
[]: 'File name too long',
[]: 'No locks available',
[]: 'Function not implemented',
[]: 'Directory is not empty',
[]: 'Too many levels of symbolic links',
[]: 'No message of desired type',
[]: 'Invalid exchange',
[]: 'Invalid request descriptor',
[]: 'Exchange full',
[]: 'No anode',
[]: 'Invalid request code',
[]: 'Device not a stream',
[]: 'No data available',
[]: 'Timer expired',
[]: 'Out of streams resources',
[]: 'Machine is not on the network',
[]: 'Object is remote',
[]: 'Link has been severed',
[]: 'Communication error on send',
[]: 'Protocol error',
[]: 'Bad message',
[]: 'Value too large for defined data type',
[]: 'File descriptor in bad state',
[]: 'Streams pipe error',
[]: 'Socket operation on non-socket',
[]: 'Destination address required',
[]: 'Message too long',
[]: 'Protocol wrong type for socket',
[]: 'Protocol not available',
[]: 'Protocol not supported',
[]: 'Socket type not supported',
[]: 'Operation is not supported',
[]: 'Network is down',
[]: 'Network is unreachable',
[]: 'Network dropped connection on reset',
[]: 'Connection timed out',
[]: 'Connection refused',
[]: 'Host is down',
[]: 'No route to host',
[]: 'Operation already in progress',
[]: 'Operation now in progress',
[]: 'Stale file handle',
[]: 'Remote I/O error',
[]: '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;
}
}