nestjs-mapped-exception
It helps handle with exception on your nestjs application.
Using this package, we can define code for our exception separating by features, where each exception will have a code (four digits), and prefix code and suffix code (see how to setup it bellow).
For example, if we have a feature called user
, a exception with code 1 and a prefix set up as ERR
, we will get the code ERR0001USE
Requirements
- NodeJS 12.0.0 or later
- NestJS 7 or later
Usage
Instalation
$ npm install -- save nestjs-mapped-exception
or yarn
$ yarn add nestjs-mapped-exception
Setup
To setup the exception to the feature module, we have import the MappedExceptionModule
in our module like this:
// user.module.ts ;;
or you can setup to the entire application
// app.module.ts ;;
You also can use environment variable to set prefix with EXCEPTION_ERROR_PREFIX= on your .env
file
After, we need to create our exception file
// user.exception.ts ;;
The status code is used for REST
context, for GraphQL
or Microservice
contex, maybe we cannot use that.
Then we need to inject our exception in the service layer like this:
(by Feature)
// user.service.ts ;
(by Root)
// user.service.ts ;;
And for the last step, we have to threat the exception inside the service using the NestJs Filters:
// user.controller.ts ;
This can be used on resolvers in GraphQl context
This way, our MappedExceptionFilter
will handle with all error generated on the service layer