https://github.com/m-ahmedk/custom-error-management
A Node.js project that centralizes error handling in an application by implementing a middleware, responsible for catching all errors thrown by the application and returning a custom error message along with the relevant HTTP status code. Additionally, all errors are logged and stored in a file.
https://github.com/m-ahmedk/custom-error-management
centralized-error-handling error-management express nodejs
Last synced: about 2 months ago
JSON representation
A Node.js project that centralizes error handling in an application by implementing a middleware, responsible for catching all errors thrown by the application and returning a custom error message along with the relevant HTTP status code. Additionally, all errors are logged and stored in a file.
- Host: GitHub
- URL: https://github.com/m-ahmedk/custom-error-management
- Owner: m-ahmedk
- License: mit
- Created: 2023-02-02T17:48:48.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-09T14:26:24.000Z (over 3 years ago)
- Last Synced: 2025-05-20T18:19:32.305Z (about 1 year ago)
- Topics: centralized-error-handling, error-management, express, nodejs
- Language: JavaScript
- Homepage:
- Size: 27.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Centralized Error Handling Middleware
This project provides a centralized error handling mechanism for a Node/Express application. The error handling is performed using a middleware that receives all errors thrown by the application and returns a custom error message along with a relevant HTTP status code. If any errors go unhandled, a default internal server error (HTTP 500) with a custom message is assigned.
The errors are also logged using Winston logger and stored in a file. The logs are automatically deleted after 3 days. This system helps to provide consistent, structured, and comprehensive error handling and reporting in the application.
Additionally, the project opens a swagger documentation (http://localhost:3000/api-docs) which displays all the available requests, their description, and the methods.
## Features
- Catch all errors thrown by the application
- Return custom error message along with relevant HTTP status code
- Assign default internal server error (HTTP 500) with custom message if any errors go unhandled
- Log all errors using Winston logger
- Store logs in a file and automatically delete after 3 days
- Consistent, structured, and comprehensive error handling and reporting
## Getting Started
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
## Prerequisites
- Node.js
- npm
## Installing
Clone the repository:
```
git clone https://github.com/m-ahmedk/centralized-error-handling.git
```
Navigate to the cloned repository:
```
cd centralized-error-handling
```
Install dependencies:
```
npm install
```
Run the project:
```
npm start
```
Open the Swagger documentation in your browser at http://localhost:3000/api-docs
## Built With
Node.js
Express
Winston
Swagger
## Contributing
If you would like to contribute to the project, please reach out to the repository owner.
## License
This project is licensed under the MIT License - see the [](https://github.com/m-ahmedk/custom-error-management/blob/main/LICENSE) file for details.