JSPM

@errorhandler/middleware

0.0.5
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 2
    • Score
      100M100P100Q35668F
    • License ISC

    Package Exports

      This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@errorhandler/middleware) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

      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:

      npm install @errorhandler/middleware

      or

      yarn add @errorhandler/middleware

      Usage

      1. Import the middleware in your app.ts file:
      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);
      1. Use the CustomError class to throw custom errors within your application:
      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();
          };
      };
      1. Use the response function for consistent response formatting:
      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.

      License

      This project is licensed under the MIT License - see the LICENSE file for details.