A lightweight and developer-friendly toolkit for robust error handling in Express.js applications. Includes ready-to-use custom error classes, an async route error handler wrapper, a global error handler middleware, and a convenient 'not found' route hand
npm install express-error-toolkit
!typescript


!minified
!minified gzip
š View on npm
A lightweight, production-ready error handling toolkit for Express.js applications ā written in TypeScript with full support for both CommonJS and ESM.
If you like the project, please give the project a GitHub ā
It provides:
- Custom error classes (NotFoundError, BadRequestError, ValidationError, etc.)
- Express middleware: globalErrorHandler, notFoundHandler
- An asyncHandler utility to handle async errors without boilerplate
- A httpError() factory function to create custom error instances easily
- isCustomAPIError() type guard for safe error type checks
---
- ā
Type-safe custom error classes
- ā
Centralized error-handling middleware
- ā
Async error wrapper for route handlers
- ā
Built-in 404 (Not Found) handler
- ā
Factory method for generating custom errors
- ā
Type guard for runtime error checking
- ā
Out-of-the-box support for both CJS and ESM
---
You can install express-error-toolkit using your favorite package manager.
``bash`
npm install express-error-toolkit
`bash`
yarn add express-error-toolkit
`bash`
pnpm add express-error-toolkit
> āļø Requires Node.js v14 or higher.
> ā¹ļø Make sure you have express installed in your project, as this toolkit is built specifically to enhance Express.js error handling.
---
`ts
import express from 'express';
import { asyncHandler } from 'express-error-toolkit';
const app = express();
app.get(
'/users/:id',
asyncHandler(async (req, res) => {
// Your async code here
throw new Error('Something went wrong!');
})
);
`
---
`ts
import { NotFoundError, BadRequestError } from 'express-error-toolkit';
app.get('/test', (req, res) => {
throw new NotFoundError('User not found');
});
`
Each custom error automatically sets the correct statusCode and name.
You can also pass optional errorDetails as a string, object, or leave it out:
`ts`
throw new BadRequestError('Invalid data', { field: 'email' });
throw new BadRequestError('Invalid input', 'Missing required field');
throw new BadRequestError('Generic client error');
---
`ts
import { notFoundHandler } from 'express-error-toolkit';
app.use(notFoundHandler);
`
This will throw a NotFoundError with the method and route info.
---
`ts
import { globalErrorHandler } from 'express-error-toolkit';
app.use(globalErrorHandler);
`
By default, the handler includes the stack trace and logs the error in development.
In production (NODE_ENV=production), both are automatically suppressed for safety.
---
To enhance developer experience during debugging, this toolkit uses ANSI escape codes ā no external dependencies like chalk required ā to make console logs more readable.
Each part of the error log is styled using a traffic light-inspired color scheme:
- š“ Error Status & Message ā Red
- š” Error Details ā Yellow (optional; string or object)
- š¢ Stack Trace ā Green (shown only if available and enabled)
> š¼ļø Example: Here's how the console might look in development mode:
---
#### ⨠Customizing the Intro Line
By default, an introductory line ("Even the best code breaks sometimes! Let's debug...") is displayed before each error block.
You can control this with the introLine option:
`ts
import { setErrorOptions } from 'express-error-toolkit';
// Disable the intro line
setErrorOptions({
introLine: false
});
// Customize the intro line
setErrorOptions({
introLine: 'šØ Debugging session begins here...'
});
`
---
You can configure the error handling behavior (e.g., hide stack traces and disable console logging even in development) using either:
`.env`
SHOW_STACK=false
LOG_ERROR=false
or directly in your code:
`ts
import { setErrorOptions } from 'express-error-toolkit';
setErrorOptions({
showStack: false,
logError: false
});
`
This overrides the default behavior (based on NODE_ENV or .env file).
---
`ts
import { httpError } from 'express-error-toolkit';
throw httpError('Something custom', 418);
`
You can also pass optional errorDetails as a string, object, or leave it out:
`ts`
throw httpError('Expectation failed', 417, { reason: 'The server could not meet the Expect header requirements' });
throw httpError('Failed Dependency', 424, 'This request relies on a previous request that failed' );
throw httpError('Unavailable for legal reason', 451);
---
`ts
import { isCustomAPIError } from 'express-error-toolkit';
if (isCustomAPIError(err)) {
console.log(err.statusCode, err.message);
// your rest of the code
}
`
---
| Error Class | Default Message | Status Code |
|------------------------|-------------------------|-------------|
| BadRequestError | Bad Request | 400 |UnauthorizedError
| | Unauthorized | 401 |ForbiddenError
| | Forbidden | 403 |NotFoundError
| | Not Found | 404 |ConflictError
| | Conflict | 409 |ValidationError
| | Unprocessable Entity | 422 |TooManyRequestsError
| | Too Many Requests | 429 |CustomAPIError
| | Internal Server Error | 500 |
---
``
āāā src/
ā āāā error/
ā ā āāā BadRequestError.ts
ā ā āāā NotFoundError.ts
ā ā āāā ...
ā āāā global-error-handler.ts
ā āāā async-handler.ts
ā āāā http-error.ts
ā āāā index.ts
āāā example/
ā āāā index.ts
āāā __tests__/
ā āāā *.test.ts
---
- Fully written in TypeScript
- Outputs:
- CommonJS: dist/index.cjsdist/index.js
- ESM: dist/index.d.ts
- Types:
---
MIT Ā© Rashedin Islam
---
This project includes http-status-toolkit`, also created by me.
We welcome contributions! Please check out the Contributing Guide.
- GitHub: dev-rashedin
- Portfolio: rashedin.dev
---
Made with ā¤ļø by Rashedin Islam