You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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?
The text was updated successfully, but these errors were encountered:
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.)
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:
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:
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?
The text was updated successfully, but these errors were encountered: