Handling exceptions and errors (Prisma ORM v6) (/docs/orm/v6/prisma-client/debugging-and-troubleshooting/handling-exceptions-and-errors)
For the complete Prisma documentation index, see llms.txt. A markdown version of any docs page is available by appending
.mdto its URL.
This page covers how to handle exceptions and errors
Location: ORM > v6 > Prisma Client > Debugging and Troubleshooting > Handling exceptions and errors
In order to handle different types of errors you can use instanceof to check what the error is and handle it accordingly.
The following example tries to create a user with an already existing email record. This will throw an error because the email field has the @unique attribute applied to it.
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
}Use the Prisma namespace to access the error type. The error code can then be checked and a message can be printed.
import { PrismaClient, Prisma } from "@prisma/client";
const client = new PrismaClient();
try {
await client.user.create({ data: { email: "alreadyexisting@mail.com" } });
} catch (e) {
if (e instanceof Prisma.PrismaClientKnownRequestError) {
// The .code property can be accessed in a type-safe manner
if (e.code === "P2002") {
console.log(
"There is a unique constraint violation, a new user cannot be created with this email",
);
}
}
throw e;
}See Errors reference for a detailed breakdown of the different error types and their codes.
Related pages
Section titled “Related pages”Debugging: This page explains how to enable debugging output for Prisma Client by setting theDEBUGenvironment variable.Troubleshooting binary size and deployment issues: This page covers how to resolve large bundle sizes, slow builds, and deployment errors caused by Prisma ORM Rust engine binaries