Skip to content

Consider replacing rejectOnNotFound #12159

Open

Description

@matthewmueller

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

Activity

  1. added
    tech/typescript
    on May 25, 2022
  2. zomars commented on May 27, 2022

    @zomars

    I don't think removing global error handlers is a good idea. We rely on custom errors for each model and removing this will cause too much code repetition in our code base, or need to replace al prisma calls with a wrapper function.

  3. zomars commented on May 27, 2022

    @zomars

    Fixing the global signature and allowing to pass our custom errors like described in #13497 seems like a more sustainable solution. But that's just me 🙏

  4. assigned and unassigned on Jun 17, 2022
  5. added 2 commits that reference this issue on Jun 20, 2022
    d27f684
    72b0ee7
  6. 7 remaining items

  7. matthewmueller commented on Jun 23, 2022

    @matthewmueller
    ContributorAuthor

    They throw NotFoundError errors. Should be accessible in the Prisma namespace, but let us know if it's not!

  8. cawa-93 commented on Jun 27, 2022

    @cawa-93

    @matthewmueller

    Should be accessible in the Prisma namespace, but let us know if it's not!

    it's not.
    зображення

  9. zomars commented on Jun 27, 2022

    @zomars

    @cawa-93 you're using findFirst I think you would need to use findFirstOrThrow new method and use latest prisma to actually catch Prisma.NotFoundError. Am I wrong @matthewmueller ?

  10. added 2 commits that reference this issue on Jun 30, 2022
    30a8151
    a1259ab
  11. added 2 commits that reference this issue on Jul 6, 2022
    b565d2c
    9fb32c1
  12. agustif commented on Aug 12, 2022

    @agustif

    I think it's Prisma.notFound

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

  13. zomars commented on Aug 12, 2022

    @zomars

    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

  14. agustif commented on Aug 12, 2022

    @agustif
  15. aqrln commented on Aug 15, 2022

    @aqrln
    Member

    @agustif the Data Proxy error is irrelevant, the one you are looking for is the second one. There's no error code, you can use the instanceof operator:

    import { Prisma } from '@prisma/client'
    
    // ...
    
    try {
      // ...
    } catch (err) {
      if (err instanceof Prisma.NotFoundError) {
        // ...
      }
    }

    We should probably improve the docs here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions