Node: Promise.catch(Error, (err) => {}) catching specific errors doesn't work

Created on 18 May 2016  Â·  5Comments  Â·  Source: nodejs/node

  • v5.10.1:
  • Darwin Kernel Version 15.4.0:
  • Promise:

I'm trying to .catch() specific errors from another promise and the following doesn't work:

...
.catch(MyCustomError, (err) => {
     // this block doesn't get called
})
.catch((err) => {
    // this block is called instead
    console.log(err instanceof MyCustomError) // false
})

using the same block of code but with Bluebird the block is executed as expected.
Any thoughts?

promises question

All 5 comments

@teckays Per spec, Promise.prototype.catch() accepts only one argument — the onRejected callback.

What Bluebird does is an non-standard extension, similar to Bluebird.promisifyAll().

@ChALkeR I would still not close this issue based on the second .catch() block because the instance of the err returned by my other promise is changed when proxied back to my initial Promise chain. So if I do:

...
.catch((err) => {
   console.log(err) // [TypeError: Cannot set property 'message' of undefined]
   console.log(err instanceof MyCustomError) // false
})

so this would definitely be a good topic for discussion.

ok @ChALkeR never mind, had a completely new test written from scratch to test this bug in particular and looks like it's not from Promise. Thanks.

Following standards makes all modern JavaScript engines very obsolete unfortunately. Bluebird is the best Promise library, I've no idea why V8 developers didn't get it as the _standard_. Design by committee vs most stars and developers satisfaction...

Relying on manual error handling can eat a lot of time. See https://stackoverflow.com/questions/42064466/instanceof-using-es6-class-inheritance-chain-doesnt-work and
https://github.com/babel/babel/issues/4058 for examples of why Bluebird is so good.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danielstaleiny picture danielstaleiny  Â·  3Comments

seishun picture seishun  Â·  3Comments

stevenvachon picture stevenvachon  Â·  3Comments

addaleax picture addaleax  Â·  3Comments

filipesilvaa picture filipesilvaa  Â·  3Comments