Promise.catch is swallowing errors

Viewed 950

I've done a lot of async coding in Node.js with callbacks and the excellent async library which works great. I'm trying to use a module that uses promises but I'm running into a problem where any errors thrown AFTER the promise are still bubbled up and caught by the promise error handler.

This makes it very difficult to debug errors as I have no idea where they will pop up and they can't be thrown and don't crash the app.

Example code below; all I want to do is to exit the promise chain and leave it behind once it has been resolved, rather than it catching all subsequent errors that aren't anything to do with it.

function one (input, callback) {

  doSomeAsyncWork(input)
  .then(function (result) {
    return callback(null, result);
  })
  .catch(function (err) {
    logError(err);
    return callback(err);
  });

}

function two (err, result) {

  if (err) { ... }

  var x = callAMethodThatThrows();
  ...

}

one('abc', two);

In this example, the method callAMethodThatThrows() throws an error, which gets bubbled up to the promise catch() block. This prevents the app from crashing and leaves it in an unknown state.

Any advice would be really appreciated, thanks.

2 Answers
Related