prisma: Unable to issue instanceof Prisma.PrismaClientValidationError on known caught PrismaClientValidationError
Bug description
When an error is caught by the try catch statement, console.log(e) shows that the error is a PrismaClientValidationError
...
PrismaClientValidationError:
Invalid `prisma.cardholder.create()` invocation:
{
data: {
...
Yet running the caught e through a validation statement will not result in true, and so will not run the enclosed code of the statement.
try {
const addCardholder = await db.cardholder.create({ data: cardholder });
} catch (e) {
if (e instanceof Prisma.PrismaClientValidationError) {
error = [true, e.message]; // this never is ran as the if statement results in false
}
}
How to reproduce
- Send a DB record create statement with known invalid schema or values
- This will result in a PrismaClientValidationError
- Pass this through a try catch statement and try to use instanceof against the Prisma.PrismaClientValidationError type
- User preference for output statement, but this is definitely not running as the array remains its predefined values of [false, undefined]
Expected behavior
No response
Prisma information
db variable represented by db.server.ts
import { PrismaClient } from "@prisma/client";
let db: PrismaClient;
declare global {
var __db: PrismaClient | undefined;
}
if (process.env.NODE_ENV === "production") {
db = new PrismaClient();
db.$connect();
} else {
if (!global.__db) {
global.__db = new PrismaClient();
global.__db.$connect();
}
db = global.__db;
}
export { db };
Environment & setup
- OS: macOS
- Database: MySQL
- Node.js version: 16.14.0, Remix 1.2.0
Prisma Version
prisma : 3.10.0
@prisma/client : 3.10.0
Current platform : darwin
Query Engine (Node-API) : libquery-engine 73e60b76d394f8d37d8ebd1f8918c79029f0db86 (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine : migration-engine-cli 73e60b76d394f8d37d8ebd1f8918c79029f0db86 (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 73e60b76d394f8d37d8ebd1f8918c79029f0db86 (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary : prisma-fmt 73e60b76d394f8d37d8ebd1f8918c79029f0db86 (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : 73e60b76d394f8d37d8ebd1f8918c79029f0db86
Studio : 0.458.0
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 10
- Comments: 21 (4 by maintainers)
I’m using version 4.11.0 and I was facing the same issue, not sure why the direct import isn’t working but this seems to fix it:
Here’s the code I’m using, as a work around. It’s based on what was posted above:
Not very pretty, and I need to expand it out for the other error types. I’m just using it as a stand-in until an actual fix is made.
Probably related, but still happening on 4.2.1:
I can’t do this, the if always returns false… on a try,catch of a
prisma.$transactionMeanwhile I had to do this:
Facing the same issue. Prisma version is 4.11.0. Sharing the example of code:
Having exact same problem, cannot catch the exception to respond properly using nestJS
Application just stops
Same issue on Prisma 6.2.1, any work around?
I am using prisma 4.11.0 and getting the same error
In the above code
instanceof PrismaClientKnownRequestErrorreturns falseDowngrading to 4.8.1 solves the problem.
I just ran into this same issue with
PrismaClientKnownRequestErrorin a Remix project with@prisma/clientversion 4.6.1.After debugging a bit, it seems the issue only occurs when
@prisma/clientis included inserverDependenciesToBundle.I think what’s happening is somehow when Remix is rebuilding the app on changes, the error object being thrown by Prisma is not an instance of the same instance of the
PrismaClientKnownRequestErrorclass.If that’s confusing, consider that this code would print false because there are two different “instances” of class
X:While this would print true:
Anyway, my solution was just to exclude
@prisma/clientfromserverDependenciesToBundleand now theinstanceofcheck works fine!@BrendanRomanDev the snippet you are showing is not what
@alethystposted as solution.runtimemodule is our private API, it can change in any version without prior warning and you are not supposed to import directly from. ImportPrismanamespace from@prisma/clientmodule and usePrisma.PrismaClientKnownRequestErrorinstead.From my side upgrading from 4.2.1 to latest (4.8.1), fixed the issue on Vitest suite.