question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consider replacing `rejectOnNotFound`

See original GitHub issue

Problem

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 and SaveX

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:5
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
zomarscommented, Aug 12, 2022

I think it’s Prisma.notFound

.catch((e) => { if (e instanceof Prisma.notFound) { throw new HttpError({ statusCode: 404, message: "User not found" }); } });

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

1reaction
agustifcommented, Aug 12, 2022

I think it’s Prisma.notFound

.catch((e) => { if (e instanceof Prisma.notFound) { throw new HttpError({ statusCode: 404, message: "User not found" }); } });

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found