neumino / rethinkdbdash

An advanced Node.js driver for RethinkDB with a connection pool, support for streams etc.
MIT License
848 stars 108 forks source link

Centralized connection-failure error handling? #363

Closed GeoffreyPlitt closed 6 years ago

GeoffreyPlitt commented 7 years ago

I have per-query error handling as you would expect in my NodeJS rethinkdb app.

However, once in a while, my database becomes unreachable and a bunch of different queries in different places (i.e. queues and other background workers) all get the "ReqlDriverError: None of the pools have an opened connection and failed to open a new one. " error, and never seem to recover properly. But a simple restart seems to fix this.

So, I'd love if I could setup on('error',...) handling at the driver/connect level, so that in a single place, I could setup a process.exit() when this happens. I wouldn't want to put this kind of error handling at every point where I'm doing queries, just once at the driver level.

Is this possible?

eden commented 6 years ago

I was able to hack something like this by overriding Term.prototype.then:

import Term from 'rethinkdbdash/lib/term'

Term.prototype.then = function (resolve, reject) {
  return this.run().catch(err => {
    // your logic here, or:
    return Promise.reject(err)
  }).then(resolve, reject)
}