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.

Failed queries not returning error details

See original GitHub issue

Bug description

When I try to insert invalid data in a table, I get the error logged to the console but I cannot retrieve it on the code, whenever I log it or send it as a request response, this is the output:

{ 
   "clientVersion": "3.8.1"
}

How to reproduce

This is where I execute the query(post() function) that I expected to get the proper error as per the docs:

user.controller.ts :

import { PrismaClient } from "@prisma/client";
import { Request, Response } from "express";

const mockData = {
    name: "test",
    last_name: "test_lastname",
    birth_date: "28/07/1999", // invalid Date
    role_id: "1" // invalid type, should be Int
}

module.exports = {
  post: async function () {
    const prisma: any = new PrismaClient();
    try {
      const result = await prisma.user.create({
        data: mockData,
      });

      console.log(result)
    } catch (err) {
      console.log(err);
    }
  },
};

Expected behavior

I expected to get an error code(P2001 for example) or some at least some message to know what the problem on that query is and then handle application behavior accordingly.

Prisma information

schema.prisma :

model user {
  id              Int      @id @default(autoincrement())
  name            String
  last_name       String
  role            role     @relation(fields: [role_id], references: [id])
  role_id         Int
  birth_date      DateTime
  created_at      DateTime @default(now())
  updated_at      DateTime @updatedAt
}

model role {
  id          Int     @id @default(autoincrement())
  description String
  user        user[]
}

Environment & setup

  • OS: Windows 10
  • Database: PostgreSQL
  • Node.js version: 16.13.1

Prisma Version

$ npx prisma -v

Environment variables loaded from .env
prisma                  : 3.8.1
@prisma/client          : 3.8.1
Current platform        : windows
Query Engine (Node-API) : libquery-engine 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at node_modules\@prisma\engines\query_engine-windows.dll.node)
Migration Engine        : migration-engine-cli 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine    : introspection-core 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary           : prisma-fmt 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash    : 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f
Studio                  : 0.452.0

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
gustavo-maurinacommented, Feb 11, 2022

Adding up to the conversation, it also surprised me that the error was an instance of PrismaClientValidationError, since according to the docs, this type of error shouldn’t have a clientVersion on it.

1reaction
gustavo-maurinacommented, Feb 14, 2022

Sorry for taking this long, if you’re still facing this issue, try this @worktechnotoil

Update after coming back from vacation and why I closed the issue:

The error being thrown was never actually wrong, it just wouldn’t appear on the terminal when I used console.log on it for some reason. When trying to access the property alone it worked, this is how I managed to get the error message and proceed to handle it properly:

import { PrismaClient } from "@prisma/client";
import { Request, Response } from "express";
import handleQueryError from "../helpers/handleQueryError.helpers";
import { PrismaClientValidationError } from "@prisma/client/runtime";

const mockData = {
    name: "test",
    last_name: "test_lastname",
    birth_date: "28/07/1999", // invalid Date
    role_id: "1" // invalid type, should be Int
}

module.exports = {
  post: async function () {
    const prisma: any = new PrismaClient();
    try {
      const result = await prisma.user.create({
        data: mockData,
      });
    } catch (err) {
      if (err instanceof PrismaClientValidationError) 
        return handleQueryError(err.message); // sending the error message directly
    }
  },
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Error handling - Apollo GraphQL Docs
Throwing errors​​ For example, it throws a GRAPHQL_VALIDATION_FAILED error whenever an incoming operation isn't valid against the server's schema. Your resolvers ...
Read more >
Dealing with errors - Power Query
This error commonly occurs when the data source is inaccessible by the user, the user doesn't have the correct credentials to access the...
Read more >
node.js - Prisma failed queries don't return proper errors
For example PrismaClientKnownRequestError has code property, but PrismaClientValidationError does not and so on. Example: try { await client.
Read more >
Troubleshoot issues writing data | InfluxDB Cloud ...
Troubleshoot failures · Check the message property in the response body for details about the error–for example, partial write error indicates rejected points....
Read more >
STL_ERROR - Amazon Redshift
Records internal processing errors generated by the Amazon Redshift database engine. STL_ERROR does not record SQL errors or messages. The information in ...
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