Consider replacing `rejectOnNotFound`
See original GitHub issueProblem
Right now there’s two ways to use rejectOnNotFound
:
1. Globally
const prisma = new PrismaClient({
rejectOnNotFound: true,
})
const prisma = new PrismaClient({
rejectOnNotFound: {
findFirst: {
User: (err) => new Error('User error'),
Post: (err) => new Error('Post error!'),
},
findUnique: {
User: (err) => new Error('User error'),
Post: (err) => new Error('Post error!'),
},
},
})
2. Locally
const result = await prisma.user.findUnique({
where: {
id: 42,
},
rejectOnNotFound: true
})
The problem with the global one is that you’d want to change the return type for the models you decide to reject. Instead of User | null
, it should be User
, since it’s going to reject not found users anyway.
Open Question: does local rejectOnNotFound adjust the signature?
Suggested solution
My vote would be to remove this API and replace it with a few new methods:
prisma.user.mustFindUnique()
prisma.user.mustFindFirst()
Then everything is explicit, local and the return types can be properly altered.
Alternatives
I’m guessing our engineers have thought of other solutions 😃
Additional context
- Entgo does something similar with
Save
andSaveX
Issue Analytics
- State:
- Created 2 years ago
- Reactions:5
- Comments:13 (4 by maintainers)
Top Results From Across the Web
Prisma Client API (Reference)
It replaces the rejectOnNotFound option. rejectOnNotFound is deprecated in v4.0.0. findFirstOrThrow retrieves the first record in a list in the same way as ......
Read more >hello I have rejectOnNotFound enabled on a global level for | Orm ...
hello I have rejectOnNotFound enabled on a global level for all findFirst and findUnique queries but it doesnt ... I think the soulution...
Read more >Type Error for When Creating Many-to-one Relation Field on ...
I am using Next.js and its api routes, Graphql, Nexus, Prisma for my web app. I got type error when I am creating...
Read more >Veritas (@CharlesCeeJay5) / Twitter
... which we introduced in Prisma 4.0.0 to replace the rejectOnNotFound option. ... made everyone believe that serverless heaven truly exists!
Read more >nexus-prisma - npm
Prisma Client rejectOnNotFound Handling; Related Issues ... If you are new to Nexus, consider reading the official Nexus tutorial before ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The thing is the
throw new HttpError
won’t be respected globally unless this has been fixed already.EDIT: seems like global errors have been deprecated
I think it’s Prisma.notFound
.catch((e) => { if (e instanceof Prisma.notFound) { throw new HttpError({ statusCode: 404, message: "User not found" }); } });