Recently I changed my code from Express to Restify. I'm honestly not sure if it used to happen before, but I guess it did.
Basically in my middleware I call a promisified method and when it resolves I call next
and do other stuff in the next middleware. When it is rejected I also want to call next
with no errors in some cases. Otherwise it must call the error middleware passing err
to next
.
somePromise()
.then(()=>{
next();
})
.catch((err)=>{
if(err.someatt) next();
else next(err)
});
It works fine with the expected results of somePromise
. The problem is that next
is bound by the then-catch
chain. And when an error is thrown in the next middleware it invokes the catch
method and calls next
again!
I found out that next has an attribute called
and when I turn it to false before calling next again I get rid of the the errors. But of course it is an antipattern. And I'm having the same problem in a different middleware that I also used promises (calling next
as expected and then calling it again in the catch
statement).
Anyone else had a problem like that?
next()
so that exception doesn't propagate to your.catch()
handler. Or you can use both arguments to.then()
so a rejection/exception in the first.then()
callback doesn't go to that catch handler. – Azarria