W
Wasp•6mo ago
neogeofun

Prisma Error Handling

I have a combination ID on a table to enforce uniqueness of entity relations. I would like to send the user a message based on the error returned by Prisma. Normally, Prisma library provides errors to handle this: https://www.prisma.io/docs/orm/prisma-client/debugging-and-troubleshooting/handling-exceptions-and-errors However, I am unable to import these Prisma errors to my server code. Is there a way to do this? Thanks.
4 Replies
kapa.ai
kapa.ai•6mo ago
Hi @neogeofun. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the knowledge sources provided, it seems that Wasp currently has a basic helper in place that can check if an Error is a Prisma client error, and convert it to an HttpError. This helper is used during the sign up flow. However, the mapping is incomplete and there is an open issue to improve Prisma client error code to HTTP status code mapping source (https://github.com/wasp-lang/wasp/issues/384). In another example, a pattern is used to catch Prisma errors during the update call and convert them to HTTP errors. This pattern checks if the error is an instance of AuthError or a Prisma error, and throws an appropriate HttpError source (https://github.com/wasp-lang/wasp/issues/567).
try {
await prisma.{= userEntityLower =}.create({ data: userFields })
} catch (e) {
if (e instanceof AuthError) {
throw new HttpError(422, 'Validation failed', { message: e.message })
} else if (isPrismaError(e)) {
throw prismaErrorToHttpError(e)
} else {
throw new HttpError(500)
}
}
try {
await prisma.{= userEntityLower =}.create({ data: userFields })
} catch (e) {
if (e instanceof AuthError) {
throw new HttpError(422, 'Validation failed', { message: e.message })
} else if (isPrismaError(e)) {
throw prismaErrorToHttpError(e)
} else {
throw new HttpError(500)
}
}
However, it seems that there is currently no direct way to import Prisma errors into your server code in Wasp. You might need to create custom actions and handle the errors manually as shown in the examples above. Please note that this is based on the information available in the provided knowledge sources and the current state of Wasp. The Wasp team is actively working on improving error handling and other features, so this might change in the future. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @neogeofun, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
neogeofun
neogeofunOP•6mo ago
Well, rather than the one above, I'd suggest using error.code as they seem to include Prisma error status codes. So it'd be:
if (error.code === 'prisma error code here') {
// do whatever needs to be done here
}
if (error.code === 'prisma error code here') {
// do whatever needs to be done here
}
Here is a full list of error codes for reference: https://www.prisma.io/docs/orm/reference/error-reference#error-codes @miho if exporting Prisma provided error handling is in the roadmap I won't bother writing a module to handle these errors like the example above and will just wait. Let me know, thanks.
miho
miho•6mo ago
However, I am unable to import these Prisma errors to my server code.
Why do you think you can't do this? 😄 You can do this:
import { Prisma } from '@prisma/client'

Prisma.PrismaClientValidationError
import { Prisma } from '@prisma/client'

Prisma.PrismaClientValidationError
neogeofun
neogeofunOP•6mo ago
VSCode wasn't importing automatically, when I typed it suggested another import, from where it couldn't be imported. I thought maybe ORM was abstracted and could not be directly accessed. Thanks.
Want results from more Discord servers?
Add your server