npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

catch-express-error

v1.0.4

Published

It is a Node.js package for handling and catching errors in Express.js applications.

Downloads

759

Readme

Catch Express Errors 🚀

Catch Express Errors is a lightweight package that provides error handling and async error catching for your Express applications. It helps you handle application errors gracefully and ensures that no errors go unnoticed.

Installation

Install the package using npm:

npm install catch-express-error

Features

✅ Global error handler for Express
✅ Catch async function errors
✅ Custom AppError class for consistent error handling
✅ Supports both TypeScript and JavaScript
✅ Compatible with CommonJS and ES modules
✅ Easily integrate with existing Express applications
✅ Lightweight and minimalistic

Usage:

AppError

Description: AppError is a custom error class designed to represent application-specific errors. It extends the built-in Error class and allows you to create instances of errors with customized properties.

Parameters:

  1. message (string): A descriptive message explaining the error.
  2. statusCode (number, optional): The HTTP status code associated with the error. Defaults to 400 (Bad Request) if not provided.
  3. details (any, optional): Additional details or data related to the error.
  4. name (string, optional): A custom name for the error. Defaults to "App Error" if not provided.
  5. code (string | number, optional): A custom error code or identifier.

catchAsync

Description: catchAsync is a higher-order function that wraps an asynchronous function to catch any errors it may throw and pass them to Express.js's error handling middleware.

Parameters:

  1. fn (Function): The asynchronous function to be wrapped. It should accept (req, res, next) parameters.

handleGlobalErrors

Description: handleGlobalErrors is an Express.js error handling middleware that centralizes error handling for your application. It handles different types of errors and provides appropriate responses, including logging.

Parameters:

  1. logger (Logger | null, optional): An optional Winston logger instance for logging errors. If not provided or set to null, errors will be logged to the console.
  2. isProduction (boolean, optional): A flag indicating whether the application is in production mode. Defaults to false if not provided.

Please note that AppError is a class, and catchAsync and handleGlobalErrors are functions that can be used as middleware in your Express.js application to handle errors in a standardized way.

Examples:

Global Error Handler

In your app.ts or app.js file:

import express from "express";
import { handleGlobalErrors, AppError } from "catch-express-error";

const app = express();

// ...

// ... Your Express routes and middleware

app.use(handleGlobalErrors()); 

// ...

You can also use a logger with the global error handler:

import { handleGlobalErrors} from "catch-express-error";

import { createLogger } from "winston";

const logger = createLogger({
  // configure your logger
});

const app = express();

// ... Your Express routes and middleware

app.use(handleGlobalErrors(logger));

Production and Development mode with and without logger.:

import { handleGlobalErrors} from "catch-express-error";
import { createLogger } from "winston";

const logger = createLogger({
  // configure your logger
});

const app = express();

// ... Your Express routes and middleware

app.use(handleGlobalErrors()); // Without 'logger' and 'development' mode

app.use(handleGlobalErrors(logger)); // With 'logger' and 'development' mode.

app.use(handleGlobalErrors(null, true)); // Without 'logger' and 'production' mode.

app.use(handleGlobalErrors(logger, true)); // With 'logger' and 'production' mode.

Catch Async Function Errors

Wrap your async route handler functions using the catchAsync function to catch any errors and pass them to the global error handler:

const { catchAsync } = require("catch-express-error");

app.get(
  "/users",
  catchAsync(async (req, res) => {
    // Your async code here
  })
);

Or

import { catchAsync, AppError } from "catch-express-error";

const signUp = catchAsync(async (req, res, next) => {
  // Your async code here
  if (err) return next(new AppError("Invalid authentication", 401)); // AppError example
});

// ...

AppError

Use the AppError class to create custom errors with consistent properties:

const { AppError } = require("catch-express-error");

const getUserData = async () => {
  if (!data) throw new AppError("User not found!", 404);
};

or

import { AppError } from "catch-express-error";

// Example usage
return next(new AppError("Please login again", 401));

License

This package is licensed under the MIT License. See the LICENSE file for more information.

Contributing

Contributions are welcome! Feel free to open issues and submit pull requests to improve this package.

Support

If you have any questions, suggestions, or need assistance, please feel free to contact the package maintainer or open an issue on the GitHub repository.