@nwire/errors
v0.7.1
Published
Typed domain errors (NotFoundError, ValidationError, ConflictError, ForbiddenError, UnauthorizedError) for uniform error handling. Use `instanceof` checks in route handlers instead of string comparisons.
Readme
@nwire/errors
Typed domain errors — uniform HTTP / queue / CLI error envelope across every transport.
What it is
A small set of typed Error subclasses (NotFoundError, ValidationError, ConflictError, ForbiddenError, UnauthorizedError) every Nwire transport recognizes. Use instanceof in handlers and middleware instead of string comparisons; each class maps to the right status code and error envelope.
Install
pnpm add @nwire/errorsWithin nwire-app
For developers using this package as part of the Nwire stack. Throw from any handler / resolver / domain function; @nwire/http maps to HTTP status, queue maps to retry/DLQ decisions, CLI maps to exit code.
import { NotFoundError } from "@nwire/errors";
import { defineAction } from "@nwire/forge";
import { z } from "zod";
const getCourse = defineAction("getCourse", {
input: z.object({ id: z.string() }),
handler: async ({ input, ctx }) => {
const course = await ctx.resolve("db").courses.findFirst({ where: { id: input.id } });
if (!course) throw new NotFoundError("Course", input.id);
return course;
},
});
// → 404 with { code: "NOT_FOUND", message: "Course not found" } over HTTPAPI
NotFoundError(entity, id?)—code: "NOT_FOUND"→ HTTP 404.ValidationError(message)—code: "VALIDATION_ERROR"→ HTTP 400.ConflictError(message)—code: "CONFLICT"→ HTTP 409.ForbiddenError(message)—code: "FORBIDDEN"→ HTTP 403.UnauthorizedError(message)—code: "UNAUTHORIZED"→ HTTP 401.
