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

prisma.$connect() not invoking error on turned off database #8154

Closed
ElectricReality opened this issue Jul 8, 2021 · 5 comments
Closed

prisma.$connect() not invoking error on turned off database #8154

ElectricReality opened this issue Jul 8, 2021 · 5 comments
Labels
bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. kind/bug A reported bug. topic: mongodb

Comments

@ElectricReality
Copy link

ElectricReality commented Jul 8, 2021

Bug description

In the case of a failed connection, prisma should be calling the error PrismaClientInitializationError. Using the following code to connect:

import Prisma from '@prisma/client';
const { PrismaClient } = Prisma;
const prisma = new PrismaClient();

async function connecttodb() {
	console.log('Connecting to the database using PRISMA CLIENT.');
	try {
		await prisma.$connect();
		console.log('Connected to the database!');
	} catch (error) {
		console.error('Failed to connect to the database.', error);
	}
}

export { prisma, connecttodb };

the line, await prisma.$connect() should throw an error when the DATABASE_URL environment is unconnectable or unreachable. However, it does not.

Though, it throws an error when a query is called:
ConnectorError(ConnectorError { user_facing_error: None, kind: RawError { code: "unknown", message: "Server selection timeout: No available servers. Topology: { Type: Unknown, Servers: [ { Address: localhost:27017, Type: Unknown, Error: No connection could be made because the target machine actively refused it. (os error 10061) }, ] }" } })

Possible related issue/s: #6431

How to reproduce

  1. Turn off the database
  2. Run the function connecttodb()
  3. Prisma will not return a error.

Expected behavior

It's expected for prisma to throw the error PrismaClientInitializationError. According to the documentation, it should be invoked if something goes wrong when the query engine is started and the connection to the database is created.
This happens either:

  • When prisma.$connect() is called OR
  • When the first query is executed

If i'm getting anything wrong in regards to the "query engine is started and the connection to the database is created". Feel free to correct me. Though, it's still best that the error should be called on connection initialization to prevent further errors in production.

Prisma information

Connection code:

import Prisma from '@prisma/client';
const { PrismaClient } = Prisma;
const prisma = new PrismaClient();

async function connecttodb() {
	console.log('Connecting to the database using PRISMA CLIENT.');
	try {
		await prisma.$connect();
		console.log('Connected to the database!');
	} catch (error) {
		console.error('Failed to connect to the database.', error);
	}
}

export { prisma, connecttodb };

Prisma schema:

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

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["mongodb"]
}

Environment & setup

  • OS: Windows 10 Pro 21H1
  • Database: MongoDB
  • Node.js version: v16.3.0

Prisma Version

Prisma Client: 2.26.0
@ElectricReality ElectricReality added the kind/bug A reported bug. label Jul 8, 2021
@janpio janpio added topic: mongodb bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. labels Jul 12, 2021
@ElectricReality
Copy link
Author

Closing since it seems to be resolved in Prisma client version 2.27.0. Thank you!

@vishalxtyagi
Copy link

Hello, I've had a same problem with prisma when using'mysql'.

@abdelhalim97
Copy link

same here i m runing prisma 5 with mysql and it doesnt show error message when the data base url is wrong
prisma 4 on Postgres is working fine

@janpio
Copy link
Member

janpio commented Aug 9, 2023

Please open a new issue and provide all the information the bug templates asks for.

@abdelhalim97
Copy link

sorry the problem i was not calling the connecttodb on any provider
its working now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. kind/bug A reported bug. topic: mongodb
Projects
None yet
Development

No branches or pull requests

4 participants