This package provides classes to throw goapptiv standard exception, also it provides exception filters to log the exception and raise an event. The event can be consumed by creating a listener.
- Create a
.npmrc
in the root folder and add the following lines.
//npm.pkg.github.com/:_authToken=TOKEN
@goapptiv:registry=https://npm.pkg.github.com/
-
Create a personal token with read:packages permission and replace the
TOKEN
with your personal token in the above mentioned file. -
Install the package using the following command
npm install @goapptiv/exception-handler-nestjs
- Add the following snippet in the main.ts
import {
AllExceptionsFilter,
BadRequestExceptionFilter,
ConflictExceptionsFilter,
ForbiddenExceptionFilter,
InternalServerErrorExceptionsFilter,
NotFoundExceptionFilter,
UnauthorizedExceptionFilter,
} from '@goapptiv/exception-handler-nestjs';
const eventEmitter = app.get(EventEmitter2);
// eventEmitter is optional
app.useGlobalFilters(
new AllExceptionsFilter(eventEmitter),
new UnauthorizedExceptionFilter(eventEmitter),
new ForbiddenExceptionFilter(eventEmitter),
new BadRequestExceptionFilter(eventEmitter),
new NotFoundExceptionFilter(eventEmitter),
new ConflictExceptionsFilter(eventEmitter),
new InternalServerErrorExceptionsFilter(eventEmitter),
);
The Following Exception comes out of the box:
- GaBadRequestException
- GaConflictException
- GaNotFoundException
- GaInternalServerErrorException
- GaUnauthorizedException
- GaForbiddenException
Example:
import { GaConflictException } from '@goapptiv/exception-handler-nestjs';
throw new GaConflictException([
{
type: 'E409_CUSTOMER_MAPPING_BRICK_ALREADY_ATTACHED_TO_HQ',
message: `brick already attached to hqcode ${mapping.hqCode} for customer ${customer._id}, source: ${mapping.source}`,
context: mapping,
},
]);
The system will raise an event once any of the exception occurs. The different types of events can be found GaExceptionEvent
class.