Mattia Romeo
05/17/2022, 6:47 PMRichard Ward
05/17/2022, 7:33 PMRichard Ward
05/17/2022, 7:35 PMP2002 from here: https://www.prisma.io/docs/reference/api-reference/error-reference#p2002Richard Ward
05/17/2022, 7:37 PMif (err instanceof Prisma.PrismaClientKnownRequestError) ensures it is a prisma error object and you can then check if it's a P2002
try {
const created = await prisma.xxx.create({data: data});
if (created) {
return "success";
}
return "not success";
} catch (err) {
if (err instanceof Prisma.PrismaClientKnownRequestError) {
if (err.code === 'P2002') {
logger.error('Unique constraint violation, xxx not be created');
return 'Unique database constraint violation';
}
}
return 'An unexpected error has occured';
}Richard Ward
05/17/2022, 7:38 PMMattia Romeo
05/17/2022, 9:39 PMP2002 as the error code but I wasn’t sure exactly what Unique constraint failed failed meant. Really appreciate the sample code too.