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.

cannot catch all error in promise chain

See original GitHub issue

my test code, just simple visit a host which not exist.

const download = require('download');

download('https://www.ahostnotexisttddddd.com.hk', {
    timeout: 100
}).then(result => {
    console.log(result);
}).catch(() => {
    console.error('error catched!')
});

out put is

error catched!
events.js:183
      throw er; // Unhandled 'error' event
      ^

RequestError: Request timed out
    at Timeout.setTimeout [as _onTimeout] (/user/test/test/node_modules/got/index.js:205:24)
    at ontimeout (timers.js:475:11)
    at tryOnTimeout (timers.js:310:5)
    at Timer.listOnTimeout (timers.js:270:5)

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:5
  • Comments:5

github_iconTop GitHub Comments

1reaction
GaryChangCNcommented, Oct 14, 2020

same question here

1reaction
manujascommented, Apr 5, 2018

You can do this ugly workarround

Promise.resolve()
.then( () => {
 return (async () => {
    try {
      return await download('https://www.ahostnotexisttddddd.com.hk', {
        timeout: 100
      })
    } catch (err) {
       throw err
    }
  })()
)
.then(result => {
  console.log(result)
}).catch((err) => {
  console.error('error catched!')
  console.log(err)
})

eddited for better manage inside of a promisechain

It is very ugly but does the job. I think that the GOT package errors are not handled properly

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why catch at the end of promise chain is not ... - Stack Overflow
Now when i call the chain, it only works in case when inBlacklist was rejected. For the others it appears as an unhandled...
Read more >
Error handling with promises - The Modern JavaScript Tutorial
Promise chains are great at error handling. ... The easiest way to catch all errors is to append .catch to the end of...
Read more >
Promise.prototype.catch() - JavaScript - MDN Web Docs
The catch method is used for error handling in promise composition. Since it returns a Promise , it can be chained in the...
Read more >
Catching and handling different types of errors in promise chains
Promise.reject to throw specified errors ... Here's where it all comes together. Both API calls have their own .catch . This way, we...
Read more >
Error handling in long Promise chains | by Arthur Xavier
.catch(setErrorState);. Where all the functions on the Promise chain such as uploadImage, saveService, etc. are functions that return a ...
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