npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-mapped-exception

v1.4.10

Published

Mapped Exceptions for NestJS

Downloads

73

Readme

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