lquixada / cross-fetch

Universal WHATWG Fetch API for Node, Browsers and React Native.
MIT License
1.66k stars 102 forks source link

Could not catch Premature close error #175

Closed ghost closed 9 months ago

ghost commented 9 months ago

I have opened related issue https://github.com/node-fetch/node-fetch/issues/1782 but I think it could be caused by using an old node-fetch version from cross-fetch version so opening an issue here as well

Reproduction

Steps to reproduce the behavior:

const fetch = require('cross-fetch');

(async () => {
  try {
    await fetch()
  } catch (e) {
    console.log(e);
  }
})();

Expected behavior

try catch block should be able to catch fetch block and instead it throws outside of the block

C:\Users\Windows\test\node_modules\node-fetch\lib\index.js:400
                                reject(new FetchError(`Invalid response body while trying to fetch ${_this4.url}: ${err.message}`, 'system', err));
                                       ^
FetchError: Invalid response body while trying to fetch https://<SOME_URL_HERE>: Premature close
    at Gunzip.<anonymous> (C:\Users\Windows\test\node_modules\node-fetch\lib\index.js:400:12)
    at Gunzip.emit (node:events:525:35)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  type: 'system',
  errno: 'ERR_STREAM_PREMATURE_CLOSE',
  code: 'ERR_STREAM_PREMATURE_CLOSE'
}

Node.js v18.16.1
ghost commented 9 months ago

Found that other library responsible for decoding response was the issue, closing