1.0.4 • Published 5 months ago

base-error-handler v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

base-error-handler npm version

base-error-handler is a simple Node.js module for handling errors in Express APIs written in TypeScript.

Installation

npm install base-error-handler

Features

  • Custom error classes that extend a base CustomError class
  • Automatic serialization of errors
  • Clean and consistent error handling
  • Easy integration with Express
  • Written in TypeScript

Usage

Import and add error handler middleware:

import express from "express";
import { errorHandler } from "base-error-handler";

const app = express();

app.use(errorHandler);

The errorHandler will catch errors, serialize the responses, and send proper error codes.

Custom Errors

Throw custom errors from route handlers:

import { NotFoundError } from "base-error-handler";

app.get("/", (req, res) => {
  throw new NotFoundError();
});

The errorHandler will catch it and handle sending the response.

Custom Errors

base-error-handler includes common custom error classes:

  • BadRequestError
  • NotAuthorizedError
  • NotFoundError
  • DatabaseConnectionError
  • RequestValidationError

Extend the base CustomError class to create custom errors.

Example App

import express from "express";
import { errorHandler, NotFoundError } from "base-error-handler";

const app = express();

app.get("/", (req, res) => {
  throw new NotFoundError();
});

app.use(errorHandler);

app.listen(3000, () => {
  console.log("Listening on port 3000!")
})

Any errors thrown will be caught by the error handler middleware.

Contributing

Pull requests are welcome!

Feel free to open issues or create pull requests for bugs and features.

License

This module is licensed under the MIT License - see the LICENSE file for details.