Add Custom Handlers

You can easily plug-in new handler to map new Prisma error codes to Http Exception.

First, you need to create a new class

import { HttpException, ImATeapotException } from '@nestjs/common';
import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library';
import { PrismaErrorHandler } from '@tradinos/cms-backend-error-handling';

export class MyAwesomePrismaErrorHandler implements PrismaErrorHandler {
    handle(error: PrismaClientKnownRequestError): HttpException {
        return new ImATeapotException();
    }
}

then you can register it using RegisterPrismaErrorHandler decorator

import { HttpException, ImATeapotException } from '@nestjs/common';
import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library';
import { RegisterPrismaErrorHandler, PrismaErrorHandler } from '@tradinos/cms-backend-error-handling';

@RegisterPrismaErrorHandler({ codes: ['Some Code'] })
export class MyAwesomePrismaErrorHandler implements PrismaErrorHandler {
    handle(error: PrismaClientKnownRequestError): HttpException {
        return new ImATeapotException();
    }
}

and you are good to go! neat!, right?

Last updated