Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lekejosh/-errorhandler
Errorhandler middleware for Express – an npm package designed to simplify error handling by easily catching errors, eliminating the need to write a complete error handler middleware from scratch.
https://github.com/lekejosh/-errorhandler
error-handling errors express expressjs javascript middleware node node-js node-module nodejs npm npm-module npm-package npm-scripts npmjs typescript
Last synced: 5 days ago
JSON representation
Errorhandler middleware for Express – an npm package designed to simplify error handling by easily catching errors, eliminating the need to write a complete error handler middleware from scratch.
- Host: GitHub
- URL: https://github.com/lekejosh/-errorhandler
- Owner: Lekejosh
- License: mit
- Created: 2024-02-26T12:44:21.000Z (7 months ago)
- Default Branch: master
- Last Pushed: 2024-02-26T13:24:15.000Z (7 months ago)
- Last Synced: 2024-09-28T15:04:59.101Z (5 days ago)
- Topics: error-handling, errors, express, expressjs, javascript, middleware, node, node-js, node-module, nodejs, npm, npm-module, npm-package, npm-scripts, npmjs, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@errorhandler/middleware
- Size: 8.7 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @errorhandler/middleware
## Description
`@errorhandler/middleware` is a middleware package designed to handle errors in Express applications. It provides a convenient way to catch and handle errors globally within your application.
## Installation
You can install the package via npm or yarn:
```bash
npm install @errorhandler/middleware
```or
```bash
yarn add @errorhandler/middleware
```## Usage
1. Import the middleware in your `app.ts` file:
```typescript
import express from "express";
import "express-async-errors";
import { errorHandler } from "@errorhandler/middleware/build/middlewares/error.middleware";export const app = express();
// Calling the middleware function
errorHandler(app);
```2. Use the `CustomError` class to throw custom errors within your application:
```typescript
import CustomError from "@errorhandler/middleware/build/utils/custom-error";const auth = (roles: string[] = []) => {
return async (req: Request, res: Response, next: NextFunction) => {
// How to use the function in your services
if (!req.headers.authorization) throw new CustomError("Unauthorized access: Token not found", 401);
next();
};
};
```3. Use the `response` function for consistent response formatting:
```typescript
import { response } from "@errorhandler/middleware/build/utils/response";
import type { Request, Response } from "express";class UserController {
async getMe(req: Request, res: Response) {
// Send response with formatted data
res.status(200).send(response("User data", req.$user));
}async updateMe(req: Request, res: Response) {
// Example of sending response after updating user
const result = await UserService.update(req.$user?.id as string, req.body);
res.status(200).send(response("User updated", result));
}
}
```## Contributing
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the [GitHub repository](https://github.com/Lekejosh/-errorHandler).
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.