question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Catch errors mid-chain?

See original GitHub issue

tl;dr

Is catching errors in the middle of a promise chain considered bad-practice? Or is there a good reason for it?

Explanation

so I just tried the following chain

doSomething()
  .then(function() {
    throw new Error('contrived');
  })
  .catch(function(err) {
    console.log('caught');
  })
  .then(function() {
    console.log('got here');
  });

And was a little confused that ‘got here’ gets printed. I guess the documentation makes this clear:

Any exception happening in a .then-chain will propagate to nearest .catch handler.

And the fact that catch is an alias for then(null, handler) makes a little more sense of the above behavior. However it still seems far from the conventional way seen in other OO languages that I’ve used so I was wondering whether this was on purpose or just a side-effect that should be avoided from an idiomatic perspective.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
petkaantonovcommented, Feb 2, 2015

Well let’s map it to synchronus code according to the rules I gave and run it:

try {
    try {
        throw new Error()
    } catch(e) { // When you call first catch, the chain before it is just
                 // a single then so that is wrapped in try
        console.log("catch 1");
    }
    console.log("got here");
    throw new Error();
} catch (e) { // When you call second catch, the chain before it is 1 then,
              // 1 catch and 2 thens, which are now above wrapped in the second try
    console.log("catch 2");
}

Logs

fiddle.jshell.net/:26 catch 1
fiddle.jshell.net/:28 got here
fiddle.jshell.net/:31 catch 2
0reactions
olsonpmcommented, Feb 2, 2015

Each .catch and .finally create an implicit .try over the full chain

If that were true, then the following chain wouldn’t print ‘got here’:

Promise.resolve()
    .then(function() {
        throw new Error();
    })
    .catch(function() {
        console.log('catch 1');
    })
    .then(function() {
        console.log('got here');
    })
    .then(function() {
        throw new Error();
    })
    .catch(function() {
        console.log('catch 2');
    });

So what functionally happens (haven’t looked into the code, only observed test behavior) is an implicit try between each catch, which I believe allows for some confusing code. I just don’t expect to see catches and finally’s in the middle of a promise chain and I expect other people with more OO backgrounds to feel similarly (though maybe it’s just me).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Where should I .catch promise errors between separate ...
If all you want is for the promise chain to abort when an error occurs, then you can just put one .catch() at...
Read more >
Error handling, "try...catch" - The Modern JavaScript Tutorial
So, try...catch can only handle errors that occur in valid code. Such errors are called “runtime errors” or, sometimes, “exceptions”.
Read more >
GRE Tunnel Recursive Routing Error - NetworkLessons.com
GRE Recursive Routing errors occur when the router tries to reach the destination of the tunnel through the tunnel itself. I'll show you...
Read more >
API to tapping Error · Issue #916 · mxcl/PromiseKit - GitHub
Hello,. For tapping value we can use get , for tapping both value and error we can use tap . But there is...
Read more >
System Error Messages Guide For Access and Edge Routers
Explanation An invalid handle was encountered in the Access IE library. ... Explanation A midchain adjacency failed to stack onto output ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found