@blaahaj/remap-errno
v1.0.1
Published
Simplify extraction and handling of ENOENT, etc from 'error.code'
Readme
@blaahaj/remap-errno
A node module to simplify the extraction and handling of ENOENT, etc from error.code.
Usage
A type to hold errno codes:
// "EPERM" | "ENOENT" | "EISDIR" | ...
import { type Errno } from "@blaahaj/remap-errno";Extract an Errno from an error:
import { errnoFrom } from "@blaahaj/remap-errno";
import { readFile } from "node:fs/promises";
await readFile("no such file!!").catch((error) => {
const errno = errnoFrom(error); // Errno | undefined
// ...
});Turn errors with specific Errno values, into non-error values:
import { remapErrno } from "@blaahaj/remap-errno";
import { readFile } from "node:fs/promises";
const readFileIfExists = remapErrno(readFile, {
ENOENT: undefined,
EPERM: undefined,
});
const content = await readFileIfExists("somefile.json", "utf-8"); // string | undefinedAny errors not listed in the mapping are left unchanged, i.e. are still thrown.
