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.

JSON.stringify loses much of error object information vs using console.log on it

See original GitHub issue

Bug description

We have a production service that uses prisma & catches an error thrown from prisma that looks like this:

{
  clientVersion: "3.4.1"
}

How to reproduce

This is the query that triggers the error to be thrown:

try{
const startDate = new Date(Date.now() - 24 * 60 * 60 * 1000);
const limit = 100;

const query = Prisma.sql`
  select url, count(*) as count
  from (
    select distinct "Status"."userIdStr", "Status".url
    from "Status"
    where "Status"."createdAt" > (to_timestamp(${startDate.getTime()} / 1000.0))
        and "Status".url is not null
    ) as distinct_user_url_statuses
  group by url
  having distinct_user_url_statuses.count > 1
  order by count desc  
  limit ${limit}
`;
const data = await prisma.$queryRaw<{ url: string; count: number }[]>(query);
} catch (error){
  // log error
}

Expected behavior

an error to be thrown with a message & code that can help me debug what’s going on

Prisma information

prisma & client 3.4.1

prisma.schema:

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

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

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

model User {
  idStr String @id 
  name String?
  statuses Status[]
  follows Follow[] @relation("Follower")
  followers Follow[] @relation("Followee")
}

model Follow {
  id Int @id @default(autoincrement())
  userIdStr String
  user User @relation("Follower", fields: [userIdStr], references: [idStr])
  followeeIdStr String
  followee User @relation("Followee", fields: [followeeIdStr], references: [idStr])
  @@unique([userIdStr, followeeIdStr])
}

model Status {
  idStr String @id
  createdAt DateTime
  userIdStr String
  retweetCount Int @default(0)
  favoriteCount Int @default(0)
  user User? @relation(fields: [userIdStr], references: [idStr])
  url String?
  rawJson Json?
}

Environment & setup

  • GCP Cloud Function
  • Database: PostgreSQL 13
  • Node.js version: node v14

Prisma Version

from my local dev environment which is not what’s running in the cloud/prod:

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:6
  • Comments:40 (14 by maintainers)

github_iconTop GitHub Comments

3reactions
bablukpikcommented, Sep 12, 2022

When this would be fixed? I’m also getting the same thing instead of the proper error message. I’m using "@prisma/client": "^4.2.0"

2reactions
whytrnocommented, Nov 2, 2022

I get same error here, i try to use simple findUniqueOrThrow function like this

try{
        const user = await prisma.user.findUniqueOrThrow({
            where: {
                id: req.params.id
            }
        })
        res.status(200).json({
            data: user
        })
    }catch(error){
        res.status(400).json({
            data: error
        })
    }

i get res like this

   {
  "data": {
    "clientVersion": "4.5.0"
  }
}

but when i throw(error) or console.log(error), i got like this

Invalid `prisma.user.findUniqueOrThrow()` invocation:

{
  where: {
    id: '5'
        ~~~
  }
}

Argument id: Got invalid value '5' on prisma.findUniqueUser. Provided String, expected Int.

I know that error happend because parameter is not Integer, but its helpfull when its return to client maybe like this, error: Got invalid value '5' on prisma.findUniqueUser. Provided String, expected Int.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Beware of Using JSON.stringify() for Logging - Level Up Coding
Recently I was working on a legacy system built on AWS Lambda and Node.js. It uses the console object along with JSON.stringify() to...
Read more >
Is it not possible to stringify an Error using JSON.stringify?
var error = new Error('simple error message'); JSON. stringify(error, function(key, value) { console. log(key === ''); // true (?) console.
Read more >
My Friend Almost Lost His Year-end Bonus Because of JSON ...
This bug is caused by Fat Head's unfamiliarity with the JSON.stringify, so here we will analyze some features of this built-in function. Basically,...
Read more >
Why does console.log hate me? - Giacomo Cerquone
So a solution you can put in place, also illustrated by MDN, is to use JSON. parse(JSON. stringify(myObj)) . The same applies to...
Read more >
JSON.parse() - JavaScript - MDN Web Docs
The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.
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