1.0.4 • Published 3 years ago

dash-test-common v1.0.4

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

commons-service

commons-service

HttpExceptionFilter

to Catch every unhandled exception (regardless of the exception type) and make the response as

{
    errors: [
        feilds: string[],
        messages: string,
        code: string
    ]
}

this Filter handle three types of errors

  • validation errors from the dto level
  • unexpected error
  • validation in the business logic but at this level, you want to handle the structure of the error manually like that
throw new ConflictException({
            fields: ['feild'],
            code: 'CONFLICTS',
            message: 'your message',
          });

How to use it on the nestjs project

  • in the main.ts file, import the module
import { HttpExceptionFilter } from '@mon-medic/common';
  • in the bootstrap function add this line
  app.useGlobalFilters(new HttpExceptionFilter());