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.

Unable to catch error in query

See original GitHub issue

Bug description

This is my query:

try {
  return await prisma.user.findUnique({
      where: {
          id: 'HQNsa'
      }
  })
} catch(e) { // this does not catch the error
  console.log(e);
}

The id column is of type @db.Uuid in Postgres so obviously this request will fail because it is not a valid UUID. This is the error:

C:\Users\...\node_modules\@prisma\client\runtime\index.js:45578
        throw new PrismaClientKnownRequestError(message, e.code, this.client._clientVersion, e.meta);
              ^
PrismaClientKnownRequestError: 
Invalid `prisma.user.findUnique()` invocation:


  Inconsistent column data: Error creating UUID, invalid length: expected one of [36, 32], found 5
    at Object.request (C:\Users\...\node_modules\@prisma\client\runtime\index.js:45578:15) {
  code: 'P2023',
  clientVersion: '3.12.0',
  meta: {
    message: 'Error creating UUID, invalid length: expected one of [36, 32], found 5'
  }
}

My problem is not that the error gets thrown. I completely understand what the problem is with my query. However, my problem is that my try / catch block is unable to catch it. I therefore think that the error gets thrown in another scope.

Is this intended? I tried many things (e.g. also wrapping the PrismaClient instantiation into a try / catch block or not putting await in front of the query) but nothing seems to work.

I know that I could check whether my input string is a UUID before querying but I would like to just catch the error.

How to reproduce

  1. Create a Prisma schema with a postgres datasource and create a simple model where the @id column is of type @db.Uuid.
  2. Push the schema to the DB
  3. Try running the following query (change model accordingly)
prisma.user.findUnique({
    where: {
        id: 'HQNsa'
    }
})
  1. Try to catch the error that occurs to prevent the server from crashing. I wasn’t able to.

Expected behavior

I would like to be able to catch errors where they occur, not have them crash my server without me being able to catch them.

Prisma information

E.g.:

datasource db {
    provider = "postgresql"
    url = env("DATABASE_URL")
}

generator client {
    provider = "prisma-client-js"
}

model User {
    id String @id @db.Uuid
    name String
}

Environment & setup

  • OS: Windows 10
  • Database: Postgres
  • Node.js version: 16.14.2

Prisma Version

prisma                  : 3.12.0
@prisma/client          : 3.12.0
Current platform        : windows
Query Engine (Node-API) : libquery-engine 22b822189f46ef0dc5c5b503368d1bee01213980 (at node_modules\@prisma\engines\query_engine-windows.dll.node)
Migration Engine        : migration-engine-cli 22b822189f46ef0dc5c5b503368d1bee01213980 (at node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine    : introspection-core 22b822189f46ef0dc5c5b503368d1bee01213980 (at node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary           : prisma-fmt 22b822189f46ef0dc5c5b503368d1bee01213980 (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash    : 22b822189f46ef0dc5c5b503368d1bee01213980
Studio                  : 0.459.0

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ludovicguethcommented, Apr 22, 2022

@janpio yes I confirm that !

1reaction
puchmcommented, Apr 22, 2022

I understand correctly you have this middleware to “brand” the model name into the result?

Yes, that is exactly what I am doing. I added the await and it works now so I am closing this. Thanks guys!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to catch error in nestjs when a query fails - Stack Overflow
But I am not sure as what could be the best of way of catching errors. entity.ts import { Entity, Column, PrimaryGeneratedColumn, ...
Read more >
Why try/catch doesn't work with failed queries? - Retool Forum
Queries in Retool return a promise so this code works correctly: const result = await someQuery.trigger().
Read more >
Error Handling in SQL Server with Try Catch
In this article we look at how to use error handling in your SQL Server code by using Try Catch statements.
Read more >
Common Hibernate Exceptions - Baeldung
Many conditions can cause exceptions to be thrown while using Hibernate. These can be mapping errors, infrastructure problems, SQL errors, ...
Read more >
How To Setup Error Handling For APIs and DB Queries
Errors can pop up and randomly break a user's flow on your app, but there are techniques to make ... Your browser can't...
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