download icon indicating copy to clipboard operation
download copied to clipboard

cannot catch all error in promise chain

Open vnoder opened this issue 7 years ago • 5 comments

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)

vnoder avatar Jan 16 '18 09:01 vnoder

It seems that p-event just only resolved first error throwed out by got stream.

vnoder avatar Jan 16 '18 09:01 vnoder

I'm having the same problem. Even wrapped in try/catch this is terminating the program as an unhandled error.

phil-andrews avatar Feb 23 '18 18:02 phil-andrews

Same here!

manujas avatar Apr 05 '18 12:04 manujas

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

manujas avatar Apr 05 '18 12:04 manujas

same question here

GaryChangCN avatar Oct 14 '20 07:10 GaryChangCN