Distinguishing errors

{
"code": "P2002",
"meta": {
"modelName": "User",
"driverAdapterError": {
"name": "DriverAdapterError",
"cause": {
"kind": "UniqueConstraintViolation",
"constraint": {
"fields": [
"email"
]
}
}
}
},
"clientVersion": "6.10.1",
"name": "PrismaClientKnownRequestError"
}
{
"code": "P2002",
"meta": {
"modelName": "User",
"driverAdapterError": {
"name": "DriverAdapterError",
"cause": {
"kind": "UniqueConstraintViolation",
"constraint": {
"fields": [
"email"
]
}
}
}
},
"clientVersion": "6.10.1",
"name": "PrismaClientKnownRequestError"
}
this is the structure of the error i get inside my try {} catch(e) {} when Prisma fails. It's very difficult to get at the cause of the error. I need to know the kind of the error (UniqueConstraintViolation) and the field (email), but because of this nesting I'm having serious difficulty getting what I need. Is there another more idiomatic way of handling this?
3 Replies
Prisma AI Help
Prisma AI Help6mo ago
You chose to debug with a human. They'll tinker with your query soon. If you get curious meanwhile, hop into #ask-ai for a quick spin!
Nurul
Nurul6mo ago
Hey! Can you elaborate on your use case? Do you want to handle different type of errors differently? You need to manually access the nested properties in the error object. There is no more idiomatic or built-in Prisma way to flatten or simplify this error structure at this time https://www.prisma.io/docs/orm/prisma-client/debugging-and-troubleshooting/handling-exceptions-and-errors
Handling exceptions and errors (Reference) | Prisma Documentation
This page covers how to handle exceptions and errors
roguehusky3
roguehusky3OP6mo ago
Hey, thanks for responding! According to thje link you posted, there should be a Prisma.PrismaClientKnownRequestError class you can import which has a code field which I could use to determine why Prisma Client failed to create a new user (email address already in use). That is my main use case

Did you find this page helpful?