UNPKG

globleerrorhandler

Version:

"Simplify error handling in Express.js using this libarary.🚀"

116 lines (79 loc) • 3.59 kB
## globle-error-handler This library provides an AppError class for creating application-specific errors and a global error handling middleware using Express. ## Installation This is a Node.js module available through the npm registry. Installation is done using the npm install command: ```bash npm install globleerrorhandler ``` ## API Import the necessary components into your application: ```JavaScript const { AppError, globleHandler } = require('globleerrorhandler'); ``` ## globleHandler Middleware The **globleHandler** function creates a global error handling middleware for Express applications. It provides options to customize the error response. Function: **globleHandler(options)** - **options** (Object): Options for customizing error response. - **stack** (boolean, optional): Include the full error stack in the response. - **onlyPath** (boolean, optional): Include only the path causing the error in the response. Returns: Express middleware function for handling errors. ## Examples **1. Customizing Error Responses** In this example, I've added the globleHandler middleware to your Express app using options: - stack: true will include the full error stack in the response. - onlyPath: true will include the only path causing the error. Now, when an error occurs, the error response will include the complete stack trace. You can customize these options according to your needs. ```JavaScript const express = require('express'); const { AppError, globleHandler } = require('globleerrorhandler'); const app = express(); // Applying the global error handling middleware with options app.use(globleHandler({ stack: true, onlyPath: true })); // Route triggering an error app.get('/users/:id', async (req, res, next) => { try { // Fetch data from a data source (e.g., database) const data = await User.findById(req.params.id); // Check if data exists if (!data) { return next(new AppError('No document found with that ID!', 404)); } // Respond with success and data res.status(200).json({ status: 'success', data: { data } }); } catch (error) { // Handle any internal errors next(new AppError('Internal server error', 500)); } }); // Starting the Express server app.listen(3000, () => { console.log('Server is running on port 3000'); }); ``` **2. Handling Invalid Paths** Here's an example demonstrating how to use the **globleHandler** middleware and **AppError** class to handle cases when an invalid path that doesn't exist on the server is accessed using the **app.all** method in Express: ```JavaScript const express = require('express'); const { AppError, globleHandler } = require('globleerrorhandler'); const app = express(); // Using the global error handling middleware app.use(globleHandler()); // Handling errors for invalid paths app.all('*', (req, res, next) => { next(new AppError(`Can't find ${req.originalUrl} on this server`, 404)); }); // Starting the Express server app.listen(3000, () => { console.log('Server is running on port 3000'); }); ``` By following these examples, you can effectively implement global error handling in your Express application using the **globleHandler** middleware and the **AppError** class. ## LICENSE MIT ## Keywords **global-error-handling** **error-management** **error-handling-library** **npm-package** **nodejs** **error-logging** **error-reporting** **exception-handling** **error-middleware** **express-error-handling**