Errors
Driver methods must throw errors with the same shape as node:fs errors. fsError() creates this shape. errnoOf() converts it to a wire error number.
import { ERRNO_CODES, errnoOf, fsError, isFsError, rangeError } from "mountx";#fsError(code, options?)
function fsError(code: ErrnoCode, options?: FsErrorOptions): FsError;This function creates an error that is byte-for-byte identical to a node:fs error. It sets the same message, code, negative errno, syscall, path, and dest fields. The error reaches the kernel without translation.
throw fsError("ENOENT", { syscall: "stat", path });
throw fsError("EXDEV", { syscall: "rename", path, dest });If a real fs call throws an error, pass that error through without a change. It already has the required shape.
#isFsError(error, code?)
function isFsError(error: unknown, code?: ErrnoCode): error is FsError;This function is a type guard. Give it a code to narrow the result to one error code.
#errnoOf(error)
function errnoOf(error: unknown): number;Transports use this function to convert a thrown value to a wire error number. Unknown values become EIO. This fallback helps each transport return exactly one reply for each request, even when a driver throws an unexpected value.
#ERRNO_CODES
This is the single errno table in the project. It contains Linux values because the values go on the wire. They do not always match values on the host. ErrnoCode is keyof typeof ERRNO_CODES.
Note
The project keeps this table in one location. The native addon reports a positive numeric errno. JavaScript converts that number to a name. This design prevents a second table in another language from becoming inconsistent.
#rangeError(name, expected, value)
This function creates Node's ERR_OUT_OF_RANGE. Drivers can use it to validate read and write arguments in the same way as node:fs.
#Next
- Loopback harness shows how to run the driver and inspect results.
- Writing a driver explains driver errors with examples.