Errorty is a comprehensive error handling package designed specifically for Express.js applications. It simplifies error management, improves debugging, and provides a robust error handling solution for your Express-based projects.
- Seamless integration with Express.js middleware
- Custom error classes with automatic HTTP status code mapping
- Centralized error handling middleware for Express applications
- Configurable logging with multiple log levels
- Stack trace display (configurable for production/development environments)
- Support for custom error classes
- Unhandled exception and rejection handling for Express apps
- Flexible configuration options tailored for Express.js environments
- Async and sync configuration methods
- Default logging and easy integration with custom loggers
Install Errorty using npm:
npm install errorty
Here's a basic example of how to integrate Errorty into your Express.js application:
import express, { Request, Response, NextFunction } from 'express';
import { ErrorHandler } from './src/utils/Handler';
import { Config } from './src/utils/Config';
import { HttpBadRequest } from './src/errors/HttpBadRequest';
import { HttpNotFound } from './src/errors/HttpNotFound';
async function setupAsyncApp() {
const app = express();
// Asynchronous initialization
try {
await Config.getInstance().init({
showStackTrace: process.env.NODE_ENV !== 'production',
logRequestDetails: true,
ErrortyResponseType: 'json',
});
await ErrorHandler.initialize();
console.log("Asynchronous initialization complete");
} catch (error) {
console.error("Async initialization failed:", error);
process.exit(1);
}
app.get('/error', (req: Request, res: Response, next: NextFunction) => {
throw new HttpBadRequest();
});
app.get('/not-found', async (req: Request, res: Response, next: NextFunction) => {
next(new HttpNotFound('Resource not found'));
});
// Not Found Handler
app.use(ErrorHandler.handleNotFound);
// Error Handler
app.use(ErrorHandler.handleError);
// Initialize unhandled exception handlers
ErrorHandler.initializeUnhandledExceptionHandlers();
return app;
}
// Start the server
setupAsyncApp().then((app) => {
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Async Server started on port ${port}`);
});
}).catch((error) => {
console.error('Failed to start async server:', error);
process.exit(1);
});
Errorty supports both asynchronous and synchronous configuration methods:
import { Config } from "errorty";
await Config.init({
showStackTrace: true,
logRequestDetails: true,
ErrortyResponseType: "json",
// ... other options
});
await ErrorHandler.initialize();
import { Config, ErrorHandler } from "errorty";
Config.initSync({
showStackTrace: true,
logRequestDetails: true,
ErrortyResponseType: "json",
// ... other options
});
ErrorHandler.initializeSync();
Use the synchronous method if you need to configure Errorty without using async/await, for example in environments that don't support top-level await.
-
logger
: Custom logger implementation (optional) -
showStackTrace
: Show stack traces in Express error responses (boolean) -
logRequestDetails
: Log detailed Express request information on errors (boolean) -
ErrortyResponseType
: 'json' or 'text' for Express responses -
errorOverrides
: Custom error classes configuration
Errorty comes with a built-in logger that integrates seamlessly with Express.js:
import { Logger, LogLevel } from "errorty";
const logger = new Logger(LogLevel.DEBUG);
app.use((req, res, next) => {
logger.info(`Received ${req.method} request to ${req.path}`);
next();
});
You can easily integrate your preferred logging library, such as Winston:
import winston from 'winston';
import { Config } from 'errorty';
await Config.configure({
logger: winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: 'error.log', level: 'error' }),
],
})
// ... other options
});
This example shows how to integrate Winston, but you can use a similar approach with other logging libraries.
Create custom error classes that integrate smoothly with Express.js:
import { HttpError } from "errorty";
import { HttpStatusCodes } from "errorty/enums";
export class CustomNotFoundError extends HttpError {
constructor(message = "Resource not found") {
super(message, HttpStatusCodes.NOT_FOUND);
this.name = "CustomNotFoundError";
}
}
// Usage in an Express route
app.get("/resource/:id", (req, res, next) => {
if (!resourceExists(req.params.id)) {
next(new CustomNotFoundError());
} else {
// ... handle the request
}
});
Contributions to improve Errorty for Express.js applications are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License