kevva / download

Download and extract files
MIT License
1.28k stars 200 forks source link

cannot catch all error in promise chain #151

Open vnoder opened 6 years ago

vnoder commented 6 years ago

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 commented 6 years ago

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

phil-andrews commented 6 years ago

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

manujas commented 6 years ago

Same here!

manujas commented 6 years ago

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

GaryChangCN commented 4 years ago

same question here