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.

A Prisma error that covers all possible errors as mentioned in `Errors reference`

See original GitHub issue

Problem

In any try/catch that runs a prisma query you’d have to check if the error is an instance of a specific prisma error in order to handle different kinds of errors and get the type-safety, for example if you want the the code or message property of an error.

If you want to catch all possible prisma errors you’d have to write a seperate if statement for all of them, for example if you want to log the error.

if (error instanceof Prisma.PrismaClientKnownRequestError) {
  log(error.code, error.message)
}

and so on...

If you dont do this, a possible error will not be logged because you are not checking if it’s an instance of. If you dont do any if statements you dont have the type safety

catch (error) {
  log(error.code) // type unknown, accepts a string;
  log(error.message) // type unknown, accepts a string
}

Suggested solution

A prisma error that covers all exceptions

catch (error) {
  if (error instanceof Prisma.Error) {
    log(error.code, error.message) // this will log any error that prisma throws + typesafety. both code and message are a string
  } 
}

Alternatives

Additional context

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:9
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
Prince-Rabadiyacommented, Jul 6, 2022

Would love a better way to handle errors. Even a property on the error object that returns just the error itself would be fantastic. I really don’t want to return:

Invalid `prisma.user.update()` invocation in
/home/leonardo/ironeko-fondamenta/.next/server/pages/api.js:127:32

  124   prisma
  125 }) => {
  126   try {
→ 127     return await prisma.user.update(
  An operation failed because it depends on one or more records that were required but not found. Record to update not found. 

To my frontend.

can specify it like this

new PrismaClient({
    errorFormat: 'minimal',
  })

refer this for more info

3reactions
system32uwucommented, Jul 5, 2022

Crazy that you can get this highly detailed error message from the engine

  20 async create(data: CreateUserDto) {
→ 21   const user = await this.prismaService.user.create({
         data: {
           email: 'mateo@nfw.gg',
           password: '$2a$10$CBE/dMtwekhH9IlocK1cPOXr9g.NfI3pCmaSmLFVOYv5yvkbBdY26',
       +   name: String,
       +   surname: String,
       ?   walletAddress?: String | null,
       ?   role?: INVESTOR | AGENT
         }
       })

Argument name for data.name is missing.
Argument surname for data.surname is missing.

Note: Lines with + are required, lines with ? are optional.

But not this from the client to just deliver it in the response:

{
  code: 123,
  meta: ['name', 'surname'],
  message: 'missing required fields'
}

Of course you can still use schema validators, but then what’s the point of using an ORM at all? You’re going to be re-defining your schema just to get the validation working

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handling exceptions and errors (Reference)
This page covers how to handle exceptions and errors.
Read more >
Error message reference
Errors that can occur include: The provided credentials for the database are invalid; There is no database server running under the provided hostname...
Read more >
Configuring error formatting (Concepts)
This page explains how to configure the formatting of errors when using Prisma Client.
Read more >
Advanced type safety (Reference)
Prisma Client provides full type safety for queries, even for partial queries or included relations. This page explains how to leverage the generated...
Read more >
Prisma Client API (Reference)
API reference documentation for Prisma Client. ... console.error(e). await prisma. ... Determines the level and formatting of errors returned by Prisma.
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