Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PrismaClientInitializationError is missing expected error code #10229

Closed
janpio opened this issue Nov 12, 2021 · 4 comments · Fixed by #13551
Closed

PrismaClientInitializationError is missing expected error code #10229

janpio opened this issue Nov 12, 2021 · 4 comments · Fixed by #13551
Assignees
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client. topic: error topic: postgresql
Milestone

Comments

@janpio
Copy link
Member

janpio commented Nov 12, 2021

Bug description

When an error is caused during initialization for a PostgreSQL database, the exception PrismaClientInitializationError does not contain the expected error message:

const { PrismaClient } = require('@prisma/client')

const prisma = new PrismaClient()

async function main() {

  try {
    const data = await prisma.$queryRaw`SELECT 1`;
  } catch (error) {
    // log error
    console.log(error)
    console.log('keys: ', Object.keys(error));
    console.log('error.errorCode: ', error.errorCode);
    console.error(JSON.stringify(error, null, 2));
  }

}

main()
  .catch((e) => {
    console.error(e)
    process.exit(1)
  })
  .finally(async () => {
    await prisma.$disconnect()
  })

with a connection string that does not work (e.g. postgresql://foo:bar@example.org:123/baz) returns:

C:\Users\Jan\Documents\throwaway\10141>node script.js
PrismaClientInitializationError: 
Invalid `prisma.queryRaw()` invocation:


  Can't reach database server at `example.org`:`123`

Please make sure your database server is running at `example.org`:`123`.
    at cb (C:\Users\Jan\Documents\throwaway\10141\node_modules\@prisma\client\runtime\index.js:38508:17)
    at async main (C:\Users\Jan\Documents\throwaway\10141\script.js:8:18) {
  clientVersion: '3.4.2',
  errorCode: undefined
}
keys:  [ 'clientVersion', 'errorCode' ]
error.errorCode:  undefined
{
  "clientVersion": "3.4.2"
}
(node:12220) UnhandledPromiseRejectionWarning: Error: Can't reach database server at `example.org`:`123`

Please make sure your database server is running at `example.org`:`123`.
    at C:\Users\Jan\Documents\throwaway\10141\node_modules\@prisma\client\runtime\index.js:36260:21
(Use `node --trace-warnings ...` to show where the warning was created)
(node:12220) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:12220) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

(Same problem for prisma.user.findMany({}) so this is not related to raw queries)

Environment & setup

  • OS: Windows
  • Database: PostgreSQL
  • Node.js version: v14.17.5

Prisma Version

3.4.2


Originally noticed via #10141

@janpio janpio added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. topic: error topic: postgresql team/client Issue for team Client. labels Nov 12, 2021
@haggalin
Copy link

haggalin commented Nov 12, 2021

Having this issue as well.

However running prisma.$connect() seems to throw the same exception but then the error code is there (P1001)

const { PrismaClient } = require('@prisma/client');

const prisma = new PrismaClient();

async function main() {
    try {
        await prisma.$connect();
        // const data = await prisma.$queryRaw`SELECT 1`;
    } catch (error) {
        // log error
        console.log(error);
        console.log('keys: ', Object.keys(error));
        console.log('error.errorCode: ', error.errorCode);
        console.log('error.code: ', error.code);
        console.error(JSON.stringify(error, null, 2));
    }
}

main()
    .catch((e) => {
        console.error(e);
        process.exit(1);
    })
    .finally(async () => {
        await prisma.$disconnect();
    });

returns:

PrismaClientInitializationError: Can't reach database server at `localhost`:`5432`

Please make sure your database server is running at `localhost`:`5432`.
    at /home/hagalin/dev/lit/api/node_modules/@prisma/client/runtime/index.js:36260:21 {
  clientVersion: '3.4.1',
  errorCode: 'P1001'
}
keys:  [ 'clientVersion', 'errorCode' ]
error.errorCode:  P1001
error.code:  undefined
{
  "clientVersion": "3.4.1",
  "errorCode": "P1001"
}
(node:461355) UnhandledPromiseRejectionWarning: Error: Can't reach database server at `localhost`:`5432`

Please make sure your database server is running at `localhost`:`5432`.
    at /home/hagalin/dev/lit/api/node_modules/@prisma/client/runtime/index.js:36260:21
(Use `node --trace-warnings ...` to show where the warning was created)
(node:461355) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:461355) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Edit: Maybe the issue is with the lazy connect behaviour?

@haggalin
Copy link

haggalin commented Apr 1, 2022

With this version:

prisma                  : 3.12.0-dev.38
@prisma/client          : 3.12.0-dev.38
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine 6f1b5b24baff9909403d1355bfee5155e02940b8 (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : migration-engine-cli 6f1b5b24baff9909403d1355bfee5155e02940b8 (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : introspection-core 6f1b5b24baff9909403d1355bfee5155e02940b8 (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt 6f1b5b24baff9909403d1355bfee5155e02940b8 (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash    : 6f1b5b24baff9909403d1355bfee5155e02940b8
Studio                  : 0.458.0
Preview Features        : interactiveTransactions

I tested it again because this version resolved issue #10240

But it still returns error codes as undefined

@danstarns
Copy link
Contributor

danstarns commented May 29, 2022

The .code property no longer exists on this type, here I have added a fix and added a test to cover that the errorCode property exists.

#13551

@mightyPay
Copy link

Please make sure your database server is running at db-postgresql-sfo3-13512-do-user-12901681-0.b.db.ondigitalocean.com:25060. at r (C:\Users\dell\Desktop\mpayProjects\mighty-warner - Copy\node_modules\@prisma\client\runtime\library.js:108:2574) at PrismaService.onModuleInit (C:\Users\dell\Desktop\mpayProjects\mighty-warner - Copy\src\prisma\prisma.service.ts:80:9) at async Promise.all (index 0) at callModuleInitHook (C:\Users\dell\Desktop\mpayProjects\mighty-warner - Copy\node_modules\@nestjs\core\hooks\on-module-init.hook.js:43:5) at NestApplication.callInitHook (C:\Users\dell\Desktop\mpayProjects\mighty-warner - Copy\node_modules\@nestjs\core\nest-application-context.js:224:13) at NestApplication.init (C:\Users\dell\Desktop\mpayProjects\mighty-warner - Copy\node_modules\@nestjs\core\nest-application.js:98:9) at NestApplication.listen (C:\Users\dell\Desktop\mpayProjects\mighty-warner - Copy\node_modules\@nestjs\core\nest-application.js:168:33) at bootstrap (C:\Users\dell\Desktop\mpayProjects\mighty-warner - Copy\src\main.ts:48:5)

facing this issue in local environment it is working fine for development and production apis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client. topic: error topic: postgresql
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants