Code Monkey home page Code Monkey logo

exception-handler-nestjs-package's Introduction

GoApptiv Standard Exception Handler NestJS

This package provides classes to throw goapptiv standard exception, also it provides exception filters to log the exception and raise an event. The event can be consumed by creating a listener.

Installation

  1. Create a .npmrc in the root folder and add the following lines.
//npm.pkg.github.com/:_authToken=TOKEN
@goapptiv:registry=https://npm.pkg.github.com/
  1. Create a personal token with read:packages permission and replace the TOKEN with your personal token in the above mentioned file.

  2. Install the package using the following command

npm install @goapptiv/exception-handler-nestjs
  1. Add the following snippet in the main.ts
import {
  AllExceptionsFilter,
  BadRequestExceptionFilter,
  ConflictExceptionsFilter,
  ForbiddenExceptionFilter,
  InternalServerErrorExceptionsFilter,
  NotFoundExceptionFilter,
  UnauthorizedExceptionFilter,
} from '@goapptiv/exception-handler-nestjs';

const eventEmitter = app.get(EventEmitter2);

// eventEmitter is optional
app.useGlobalFilters(
  new AllExceptionsFilter(eventEmitter),
  new UnauthorizedExceptionFilter(eventEmitter),
  new ForbiddenExceptionFilter(eventEmitter),
  new BadRequestExceptionFilter(eventEmitter),
  new NotFoundExceptionFilter(eventEmitter),
  new ConflictExceptionsFilter(eventEmitter),
  new InternalServerErrorExceptionsFilter(eventEmitter),
);

Usage

The Following Exception comes out of the box:

  1. GaBadRequestException
  2. GaConflictException
  3. GaNotFoundException
  4. GaInternalServerErrorException
  5. GaUnauthorizedException
  6. GaForbiddenException

Example:

import { GaConflictException } from '@goapptiv/exception-handler-nestjs';

throw new GaConflictException([
  {
    type: 'E409_CUSTOMER_MAPPING_BRICK_ALREADY_ATTACHED_TO_HQ',
    message: `brick already attached to hqcode ${mapping.hqCode} for customer ${customer._id}, source: ${mapping.source}`,
    context: mapping,
  },
]);

The system will raise an event once any of the exception occurs. The different types of events can be found GaExceptionEvent class.

exception-handler-nestjs-package's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.