prisma: Help developers better handle SQL errors
Problem
We sometimes want to handle SQL errors differently to return a proper error to the frontend. For example, we might want to display email already used if a unique violation is detected on the field. The current situation is that we have to check against an obscure error code (P2002 for example for unique validation failure) and then go into meta.target to get which field broke. Its never easy to extract meaningful stuff from ORM(-like) tools despite being a very common operation and I think it could be a factor of differentiation for prisma.
Propositions
I see two possible way of dealing more gracefully with that:
- Create more custom error types in javascript and use
instanceofto catch them. This would look like:try { const user = await prisma.user.create({ data }); return res.status(204).send(); } catch (err) { if (err instanceof PrismaUniqueViolationError) { return res.status(400).send('DUPLICATE EMAIL'); } return res.status(400).send('BAD REQUEST'); } - Create an enum of errors. This would look like
try { const user = await prisma.user.create({ data }); return res.status(204).send(); } catch (err) { if (err.code === PrismaErrors.UniqueViolation) { return res.status(400).send('DUPLICATE EMAIL'); } return res.status(400).send('BAD REQUEST'); }
I also think there should be helpers to extract the problematic fields from the error. Something like:
if (err.code === PrismaErrors.UniqueViolation) {
const duplicateFields = extractErrorFields(err);
return res.status(400).json({ code: 'unique_violation', fields: duplicateFields});
}
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 166
- Comments: 19 (6 by maintainers)
If possible, I’d like to have something like this which I’ve initially proposed in prisma/prisma-client-js#720:
Any updates on the status of this?
I published a package to handle this feature https://github.com/vinpac/prisma-error-enum.
I can make a PR to integrate this into prisma. Should I?
I thought on an another solution that I may publish too.
The query engine now adds more detailed error types. The next step will be to add the client side implementation.
My comments on this original issue:
error.codeproperty is a good ideaI see
metais still typed as an object. Are there any plans to addtargetto it in the interface ?Thanks for looking into it @vinpac! We are looking into this right now already, so I fear that your package will then be obsolete. Until then, definitely good work! We need at least one sprint (2 weeks) to get a solution out but will notify you once we have something.
any updates on this?
With client version
3.8.1the error.meta.target property contains the field name.Are there any plans to also have the regarding table name in there? I would really appreciate it, since it also helps narrow down and improve an eroor message.
Also the meta object is of type
object, at least in the version I’m using. Having this typed would also be awesome 😄Below is the content of https://github.com/prisma/prisma/issues/2122 which I opened after this issue (I didn’t find this issue by searching for “error messages”)
Problem
error.name = 'Error'error.messagecan vary a lot depending on the configurederrorFormat. This makes it hard to consistently handle messages.Solution
errorFormatconfig.error.shortMessageExamples
Hey, does someone have any updated on this?