Skip to content
/ errors Public

A package for creating consistent custom errors for backend development. Supports standard HTTP errors, a base BaseError class, and fully custom errors.

License

Notifications You must be signed in to change notification settings

grinwiz/errors

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

@grinwiz/errors

A Node.js package for creating consistent custom errors for backend development.
Supports standard HTTP errors, a base BaseError class, and fully custom errors.


Features

  • Standard HTTP errors: BadRequestError, NotFoundError, UnauthorizedError, etc.
  • Base BaseError for custom extensions.
  • CustomError for dynamic or one-off errors with any statusCode and errorCode.
  • Supports data metadata for debugging or logging.
  • Fully compatible with Express, Fastify, or any Node.js backend.

Installation

npm install @grinwiz/errors

or

yarn add @grinwiz/errors

Usage

const { NotFoundError, BadRequestError, CustomError } = require('@grinwiz/errors);

Standard HTTP 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 }
}

Custom Errors

throw new CustomError("Payment required", 402, "PAYMENT_REQUIRED", { plan: "premium" })

Output:
{
  name: "CustomError",
  statusCode: 402,
  errorCode: "PAYMENT_REQUIRED",
  message: "Payment required",
  data: { plan: "premium" }
}

Available Errors

  • BadRequestError400
  • UnauthorizedError401
  • ForbiddenError403
  • NotFoundError404
  • ConflictError409
  • InternalServerError500
  • CustomErrorfully customizable

Extend and Customize

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")

License

MIT

About

A package for creating consistent custom errors for backend development. Supports standard HTTP errors, a base BaseError class, and fully custom errors.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published