Node.js 'request' unhandled error, node:events:368

Viewed 529

I'm getting a reoccuring error that crashes my program. In the program, I'm consistantly making async requests (one by one I should say) to download specific images off the internet, this is my code for the request:

    const urlDownload = (uri, filename, callback) => {
    try {
        console.log('start url');
        console.log(uri);
        request.head(uri, function(err, res, body){
            if(err) {
                console.log('request error!');
                console.log(err);
            } else {
                request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
                console.log('end url');
            }
        //   console.log('content-type:', res.headers['content-type']);
        //   console.log('content-length:', res.headers['content-length']);

        });
    } catch(e) {
        console.log('urlDownload issue');
        console.log(e);
    }
}

Now, sometimes it doesn't crash, but I feel like 50% or more of the times it does with this exact error:

node:events:368
      throw er; // Unhandled 'error' event
      ^

Error: aborted
    at connResetException (node:internal/errors:691:14)
    at TLSSocket.socketCloseListener (node:_http_client:407:19)
    at TLSSocket.emit (node:events:402:35)
    at node:net:687:12
    at TCP.done (node:_tls_wrap:580:7)
Emitted 'error' event on Request instance at:
    at Request.onerror (node:internal/streams/legacy:62:12)
    at Request.emit (node:events:390:28)
    at IncomingMessage.<anonymous> (/home/mrz/Desktop/DEVELOPMENT/node_modules/request/request.js:1079:12)
    at IncomingMessage.emit (node:events:390:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'ECONNRESET'
}

I don't know what to do, tried to search about it and found nothing really, nothing wrong with the URL it tries to download too!

based on the console logs I have made, it seems like it crashes AFTER it ends the request process successfully I think, because I do get the output, and then it crashes, that's just speculations, I have no idea what's going on.

0 Answers
Related