Skip to main content

ConnectionLostError

Extends the built-in Error. Reported when a connection's socket closes without anyone asking it to — the backend was terminated by an administrator, a failover, a network fault — as opposed to a close() the application itself called. Constructed internally; you do not create it yourself.

One object reports the same event everywhere it's visible, so a caller holding two of them (say, from a query rejection and a 'close' handler) can pair them by identity:

  • The reason on Connection's 'close' event.
  • The second argument of Pool's 'destroy' event, for a pooled connection that died rather than one evicted normally.
  • Pool's 'error' event.
  • The rejection of whatever query was in flight on the connection when it happened.
pool.on('error', err => {
if (err.code === '08006') log.warn('pooled connection lost', err.processID);
else log.error('could not open a connection', err);
});

Properties​

KeyTypeReadonlyDescription
messagestringfalseAlways "Connection terminated unexpectedly". The text is deliberately generic and matches what other drivers report for the same event, so code branching on the message keeps working. What happened to this connection is in the other fields
codestringtrueAlways '08006' (connection_failure), a SQLSTATE-shaped value synthesized here. The server itself never sends it, since it cannot report its own connection going away. Lets a listener branch without instanceof, which fails across duplicated copies of the package
processIDnumberfalseThe backend's process id, as it was before the connection reset
causeunknownfalseThe underlying socket error, when there was one — a clean FIN leaves none

Telling it apart from an ordinary disconnect​

A close() you called still rejects any in-flight call with a plain Error('Connection closed'). Nothing was lost there; the caller raced a shutdown it requested. ConnectionLostError only shows up for a close nobody asked for, so code === '08006' reliably means something went wrong, not that the application shut the connection down on purpose.