Skip to content

Uncatchable error, when use Promise #2514

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

Open
dailysse opened this issue Apr 8, 2021 · 2 comments
Open

Uncatchable error, when use Promise #2514

dailysse opened this issue Apr 8, 2021 · 2 comments

Comments

@dailysse
Copy link

dailysse commented Apr 8, 2021

To demonstrate the error, let's take the file - https://github.com/brianc/node-postgres/blob/master/packages/pg/lib/connection.js AND modify query function in class Connection

Modified query Function:

query(text) {
    this.stream.end();
    this._send(serialize.query(text))
}

We artificially throw an error when we try to query the database. We close the connection to the database BEFORE sending the request. As expected, we get an error:
Error: Connection terminated unexpectedly

BUT!

If we use this code:

(async () => {
    const client = new pg.Client();
    await client.connect();
    await client.query('SELECT 8!');
})().catch(e => {
    
});

We will not get an error in the catch. The whole process will crash

This is due to the _handleErrorEvent function and this.emit ('error', err) in it in the file https://github.com/brianc/node-postgres/blob/master/packages/pg/lib/client.js

P.S.

Again, this is an artificial example. On a real project, this error occurs due to a bad network connection (for example). And every time you have to restart the node.js process.

How can I fix this problem?

@charmander
Copy link
Collaborator

client.on('error', (error) => {
    // client can never be used again, do something about that
});

A pg.Pool will partially handle the “do something about that” part for you even if you only want one client, but you still have to attach and detach an error listener (potentially empty) for now if using pool.connect(). (pool.query() doesn’t need any special error handling though.)

@abenhamdine
Copy link
Contributor

for more explanations, see #1324
and notably #1324 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants