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());