@jazim/errors
v1.0.0
Published
Typed application and HTTP exceptions for Node.js
Maintainers
Readme
@jazim/errors
Typed application and HTTP exceptions for Node.js. Dual ESM / CommonJS.
Install
pnpm add @jazim/errorsUsage
import {
AppException,
HttpException,
isAppException,
isHttpException,
} from "@jazim/errors";
throw new AppException({
message: "Something went wrong",
code: "UNEXPECTED",
});
throw new HttpException(404, {
message: "User not found",
code: "USER_NOT_FOUND",
});Logging with Pino
Pass the error instance so Pino can serialize the native stack:
logger.error(err);
// or
logger.error({ err }, "request failed");Use err.toJSON() for HTTP response bodies (no stack included).
Type guards
try {
// ...
} catch (err) {
if (isHttpException(err)) {
return c.json(err.toJSON(), err.statusCode);
}
if (isAppException(err)) {
// handle operational app errors
}
throw err;
}