tune-up-library
v1.1.27
Published
A middleware for error handling in Express
Downloads
223
Readme
tune-up-library
A middleware for error handling in Express applications.
Installation
Install the package using npm:
npm install tune-up-library
Features
- Custom error classes for common HTTP errors
- Express middleware for centralized error handling
- TypeScript support
Usage
Importing
import {
errorHandler,
BadRequestError,
NotFoundError,
UnauthorizedError
} from 'tune-up-library';
Error Handler Middleware
Add the error handler middleware to your Express application:
import express from 'express';
import { errorHandler } from 'tune-up-library';
const app = express();
// Your routes and other middleware
app.use(errorHandler);
Using Custom Error Classes
You can use the custom error classes in your route handlers or other middleware:
import { BadRequestError, NotFoundError, UnauthorizedError } from 'tune-up-library';
app.get('/example', (req, res, next) => {
try {
// Your logic here
if (someCondition) {
throw new BadRequestError('Invalid input');
}
if (anotherCondition) {
throw new NotFoundError('Resource not found');
}
if (yetAnotherCondition) {
throw new UnauthorizedError('Unauthorized access');
}
// ...
} catch (error) {
next(error);
}
});
API Reference
errorHandler(err, req, res, next)
Express middleware that handles errors and sends appropriate responses.
BadRequestError
Custom error class for 400 Bad Request errors.
NotFoundError
Custom error class for 404 Not Found errors.
UnauthorizedError
Custom error class for 401 Unauthorized errors.
License
ISC
Author
ali
Version
1.1.25
Dependencies
- express: ^4.19.2
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Issues
If you find a bug or have a suggestion, please file an issue on the GitHub repository.