Skip to content

feat(client): Export NotFoundError in Prisma namespace - #14076

Merged
aqrln merged 3 commits into
prisma:mainfrom
lodi-g:feat/export-NotFoundError
Jul 6, 2022
Merged

aqrln merged 3 commits into
prisma:mainfrom
lodi-g:feat/export-NotFoundError

Conversation

@lodi-g

@lodi-g lodi-g commented Jun 30, 2022 •

Copy link
Copy Markdown
Contributor

The NotFoundError used by findXOrThrow is not available in the Prisma namespace, which makes it hard to perform error verification.

This PR adds the necessary code to export the NotFoundError, both the type declaration and the runtime code.

Ref: #12159 (comment)

@lodi-g
lodi-g requested review from a team and aqrln and removed request for a team June 30, 2022 12:37
@CLAassistant

CLAassistant commented Jun 30, 2022 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@aqrln aqrln left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@aqrln
aqrln self-requested a review June 30, 2022 14:18

@aqrln aqrln left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the second look, I think it will fail at run time because it's still missing in the JS bundle — you have only added it to the TS typings and the browser bundle stub, as far as I can tell.

@aqrln aqrln self-assigned this Jun 30, 2022
@aqrln aqrln added tech/typescript Issue for tech TypeScript. team/client labels Jun 30, 2022
@lodi-g

lodi-g commented Jun 30, 2022 •

Copy link
Copy Markdown
Contributor Author

Hi @aqrln,

I have tested the runtime by copy/pasting the basic-sqlite reproduction and replacing the script.ts with the following code:

import { PrismaClient, Prisma } from '.prisma/client'

async function main() {
  const prisma = new PrismaClient()

  try {
    const user = await prisma.user.findUniqueOrThrow({
      where: { email: '[email protected] ' },
    })

    console.log({ user })
  } catch (e: unknown) {
    if (e instanceof Prisma.NotFoundError) {
      console.error('The error is an instance of Prisma.NotFoundError')
    } else {
      console.error('Unknown error')
    }
  }

  console.log('------------')

  try {
    throw new Prisma.NotFoundError('message')
  } catch (e: unknown) {
    console.error(e)
  }
}

void main().catch((e) => {
  console.log(e.message)
  process.exit(1)
})

I have tested it using the following commands:

❯ rm -rf node_modules && pnpm i && pnpx prisma generate && pnpx ts-node index.ts
Scope: all 2 workspace projects
..                                       | Progress: resolved 16, reused 16, downloaded 0, added 0, done

dependencies:
+ @prisma/client 0.0.0 <- ../../packages/client

devDependencies:
+ @types/node 17.0.21
+ prisma 0.0.0 <- ../../packages/cli
+ ts-node 10.8.1
+ typescript 4.7.4
Prisma schema loaded from prisma/schema.prisma

✔ Generated Prisma Client (0.0.0 | library) to ./node_modules/.prisma/client in 152ms
You can now start using Prisma Client in your code. Reference: https://pris.ly/d/client
\```
import { PrismaClient } from './node_modules/.prisma/client'
const prisma = new PrismaClient()
\```
The error is an instance of Prisma.NotFoundError
------------
NotFoundError: message
    at main (/private/tmp/prisma/reproductions/not-found-error/index.ts:23:11)

In my eyes, it seems to work, but I may be missing something?

Thanks

@aqrln

aqrln commented Jul 4, 2022 •

Copy link
Copy Markdown
Member

@lodi-g sorry, I haven't had time to take another look yet. I'll get back to you tomorrow.

In the meantime, would you mind adding small tests for this in packages/client/tests/functional/findFirstOrThrow/tests.ts and packages/client/tests/functional/findUniqueOrThrow/tests.ts? No worries if you don't want to or don't have time for it, I can do it myself later.

And thank you once again for your contribution, we'll get it merged soon!

@aqrln
aqrln force-pushed the feat/export-NotFoundError branch from 2273daf to afabab9 Compare July 6, 2022 13:31
@aqrln aqrln added this to the 4.1.0 milestone Jul 6, 2022
@aqrln

aqrln commented Jul 6, 2022 •

Copy link
Copy Markdown
Member

On the second look, I think it will fail at run time because it's still missing in the JS bundle — you have only added it to the TS typings and the browser bundle stub, as far as I can tell.

Sorry, I was confused by the diff in the snapshots in the exhaustive-schema test. It's strange that we snapshot the browser stub there but not the regular JS bundle. I may be lacking some context, but it looks unexpected to me. Either way, that would be out of scope of this PR.

I've changed some tests to also check if the thrown error is an instance of Prisma.NotFoundError instead of just checking the error message.

@aqrln
aqrln merged commit 9fb32c1 into prisma:main Jul 6, 2022
@lodi-g

lodi-g commented Jul 6, 2022

Copy link
Copy Markdown
Contributor Author

Thanks @aqrln!
I tried to make the tests but unfortunately I could not understand how to run a single test so I gave up 😓

@aqrln

aqrln commented Jul 6, 2022

Copy link
Copy Markdown
Member

No worries! Sorry that it was confusing, this indeed is not documented in the testing guide, as far as I can see. We should definitely improve it!

FWIW, it would be, for example, pnpm test:functional findUniqueOrThrow (or pnpm --filter @prisma/client test:functional findUniqueOrThrow if running it from the project root).

@solomonhawk

solomonhawk commented Jul 7, 2022 •

Copy link
Copy Markdown

Great timing, was just reaching for Prisma.NotFoundError today. Is this slated to be released in 4.1.0 (around the end of July)? Tags indicate yes, but I wanted to inquire nonetheless. :)

Thank you all!

@aqrln

aqrln commented Jul 7, 2022

Copy link
Copy Markdown
Member

@solomonhawk yes, it's going to be released in 4.1.0 on July 19.

It's also available in dev versions starting with 4.1.0-dev.38 if you want to test it, but keep in mind that using pre-release versions is not recommended in production and there are no stability guarantees about them.

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

Labels

tech/typescript Issue for tech TypeScript.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants