1.4.10 • Published 4 years ago

nestjs-mapped-exception v1.4.10

Weekly downloads
79
License
MIT
Repository
github
Last release
4 years ago

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

import { UserException } from './user.exception';
import { MappedExceptionModule } from 'nestjs-mapped-exception';

@Module({
  imports: [
    MappedExceptionModule.forFeature(UserException, {
      prefix: 'USER_ERROR_',
    }),
  ],
  ...
})
export class UserModule {}

or you can setup to the entire application

// app.module.ts

import { UserException } from '.modules/user/user.exception';
import { MappedExceptionModule } from 'nestjs-mapped-exception';

@Module({
  imports: [
    MappedExceptionModule.forRoot([UserException], {
      prefix: 'APP_ERROR_',
    }),
  ],
  ...
})
export class AppModule {}

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

import { MappedExceptionItem } from 'nestjs-mapped-exception';
import { HttpStatus } from '@nestjs/common';

export class UserException {
  MY_CUSTOM_ERROR: MappedExceptionItem = {
    message: 'This is my custom error',
    code: 1,
    statusCode: HttpStatus.BAD_REQUEST,
  };
}

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

import { MappedException } from 'nestjs-mapped-exception';

@Injectable()
export class UserService {
  constructor(private readonly exception: MappedException<UserException>) {}

  myMethodException() {
    this.exception.ERRORS.MY_CUSTOM_ERROR.throw();
  }
}

(by Root)

// user.service.ts

import { Inject } from '@nestjs/common';
import { MappedException } from 'nestjs-mapped-exception';

@Injectable()
export class UserService {
  constructor(
    @Inject('UserException')
    private readonly exception: MappedException<UserException>,
  ) {}

  myMethodException() {
    this.exception.ERRORS.MY_CUSTOM_ERROR.throw();
  }
}

And for the last step, we have to threat the exception inside the service using the NestJs Filters:

// user.controller.ts

import { MappedExceptionFilter } from 'nestjs-mapped-exception';

@UseFilters(MappedExceptionFilter)
export class UserController {
  // ...
}

This can be used on resolvers in GraphQl context

This way, our MappedExceptionFilter will handle with all error generated on the service layer

1.4.10

4 years ago

1.4.9

4 years ago

1.4.8

4 years ago

1.4.6

4 years ago

1.4.5

4 years ago

1.4.7

4 years ago

1.4.4

4 years ago

1.4.3

4 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

0.0.0

4 years ago