A Node.js package for creating consistent custom errors for backend development.
Supports standard HTTP errors, a base BaseError class, and fully custom errors.
- Standard HTTP errors:
BadRequestError,NotFoundError,UnauthorizedError, etc. - Base
BaseErrorfor custom extensions. CustomErrorfor dynamic or one-off errors with anystatusCodeanderrorCode.- Supports
datametadata for debugging or logging. - Fully compatible with Express, Fastify, or any Node.js backend.
npm install @grinwiz/errors
or
yarn add @grinwiz/errors
const { NotFoundError, BadRequestError, CustomError } = require('@grinwiz/errors);try {
throw new NotFoundError("User not found", { userId: 123 })
} catch (err) {
console.error(err)
}
Output:
{
name: "NotFoundError",
statusCode: 404,
errorCode: "NOT_FOUND",
message: "User not found",
data: { userId: 123 }
}throw new CustomError("Payment required", 402, "PAYMENT_REQUIRED", { plan: "premium" })
Output:
{
name: "CustomError",
statusCode: 402,
errorCode: "PAYMENT_REQUIRED",
message: "Payment required",
data: { plan: "premium" }
}BadRequestError→400UnauthorizedError→401ForbiddenError→403NotFoundError→404ConflictError→409InternalServerError→500CustomError→ fully customizable
You can extend BaseError to create your own reusable error classes:
const { BaseError } = require('@grinwiz/errors');
class MyCustomError extends BaseError {
constructor(message, data = {}) {
super(message, { statusCode: 418, errorCode: "I_AM_A_TEAPOT", data });
}
}
throw new MyCustomError("I'm a teapot")MIT