ts-strict-error
v1.0.0
Published
Descriptive and unpackable errors for easier error handling
Downloads
1
Readme
ts-strict-error
ts-strict-error is an extremely simple little library that provides some nice ergonomics for creating errors in Typescript that you can ts-pattern against.
Usage
import { createError, None } from "ts-strict-error";
const NetworkError = createStrictError("NetworkError");
const NonOkStatusCode = createStrictError<
"NonOkStatusCode",
None,
{ code: number }
>("NonOkStatusCode");
const FetchError = createStrictError<
"Fetch",
InstanceType<typeof NetworkError> | InstanceType<typeof NonOkStatusCode>
>("Fetch");
match(new FetchErr({ from: new NonOkStatusCode({ context: { code: 500 } }) }))
.with({ name: "Fetch", cause: { name: "NetworkError" } }, () => {
console.log("No network connection available");
})
.with({ name: "Fetch", cause: { name: "NonOkStatusCode" } }, ({ cause }) => {
console.log(`Received non-OK status code: ${cause.context.code}`);
})
.exhaustive();