0

i need to get the error from any query that fire an error is there a way to get the error event in one place?

i am using mongodb v3.6 and mongodb driver v2.2

i have tried using

db.on('error',console.error)

but that didn't fired , my guess is that it only happens to the db error itself and not the quires

Amit Wagner
  • 3,134
  • 3
  • 19
  • 35
  • your guess is correct. error event handler is for errors from mongodb itself. As mongo extends EventEmitter, you can pass all errors from query to your handler. Check this answer https://stackoverflow.com/a/25718939/5821253 – Enthusiastic Developer Oct 18 '18 at 12:27

1 Answers1

0

The only way to achieve that is to wrap the mongodb functions and implement your own error handling.


Example :

function find(...args) {
  return new Promise((resolve, reject) => {
    mongodb.find(...args)
      .then(resolve)
      .catch((err) => {
        // Call an high level error handler
        declareError(err);

        reject(err);
      });
  });
}
Orelsanpls
  • 22,456
  • 6
  • 42
  • 69