nestjs-api-tutorial icon indicating copy to clipboard operation
nestjs-api-tutorial copied to clipboard

Error Handling for Prisma

Open vibbs opened this issue 2 years ago • 0 comments

the import from the tutorial

import { PrismaClientKnownRequestError } from '@prisma/client/runtime';

Is not working as expected, instead when I import like below as per the documentation it works fine:

import { Prisma } from '@prisma/client';

// and try to match error instance in this way
if (error instanceof Prisma.PrismaClientKnownRequestError) {
        console.log(error.message);
        if (error.code === 'P2002') {
          throw new ForbiddenException('Credentials taken');
        }
      }

Hope this helps if anyone following along faces the same issue.

Doc link referred: https://www.prisma.io/docs/concepts/components/prisma-client/handling-exceptions-and-errors

vibbs avatar Apr 06 '23 14:04 vibbs