# catch-express-error

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

Latest version **1.0.5** (published 2026-03-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install catch-express-error
pnpm add catch-express-error
yarn add catch-express-error
bun add catch-express-error
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2026-03-28 |
| First published | 2023-07-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 11 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | nextyfine-dev |
| Maintainers | nextyfine-dev |
| Keywords | error handler, error handling middleware, express, middleware, validation, error handling, async, typescript, request, response, utility, utilities, validation middleware, express middleware, async error handling, request validation, response handling, error management, error wrapper, catch errors, express errors, request errors, response errors, request validation middleware, response error handling, express error handler |

## Links

- npm: https://www.npmjs.com/package/catch-express-error
- Repository: https://github.com/nextyfine-dev/catch-express-errors
- Homepage: https://github.com/nextyfine-dev/catch-express-errors#readme
- Issues: https://github.com/nextyfine-dev/catch-express-errors/issues
- npm.io page: https://npm.io/package/catch-express-error

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.5 (latest) — 2026-03-28
- 1.0.4 — 2023-12-16
- 1.0.3 — 2023-10-06
- 1.0.2 — 2023-10-04
- 1.0.1 — 2023-10-04
- 1.0.0 — 2023-07-13
- 0.1.10-b — 2023-07-13
- 0.1.10-a — 2023-07-13
- 0.1.10 — 2023-07-13
- 0.1.9 — 2023-07-13
- 0.1.7 — 2023-07-13
- 0.1.6 — 2023-07-13
- 0.1.5 — 2023-07-13
- 0.1.4 — 2023-07-13
- 0.1.3 — 2023-07-13
- … 3 more at https://npm.io/package/catch-express-error/versions

## 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:

```shell
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:

```typescript
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:

```typescript
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.:

```typescript
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:

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

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

Or

```typescript
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:

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

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

or

```typescript
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](https://opensource.org/licenses/MIT). See the [LICENSE](./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.

---
_Source: https://npm.io/package/catch-express-error · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
