ladjs / superagent

Ajax for Node.js and browsers (JS HTTP client). Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.
https://ladjs.github.io/superagent/
MIT License
16.56k stars 1.33k forks source link

Unhandled exceptions occur even when having every superagent request handled #1746

Open flareman opened 1 year ago

flareman commented 1 year ago

I'm using superagent v8.0.0 to manage requests for a homebridge plugin that I have written (homebridge-caddx-interlogix). I have written a helper function to handle requests, that looks like this:

  private async makeRequest(address: string, payload = {}) {
    let response: superagent.Response = undefined;
    try {
      response = await superagent.post(address).type('form').send(payload).redirects(0);
    } catch (error: superagent.Response.error) {
      let attemptLoginFirst = false;
      if ((error.status / 100 | 0) == 3)
        attemptLoginFirst = true;
      if (attemptLoginFirst == false && Utilities.ERROR_CODES.has(error.code) == false)
        throw(error);
        await Utilities.delay(retryDelayDuration);
      if (attemptLoginFirst) {
        await this.login();
        (<any>payload)['sess'] = this.sessionID;
      }
      response = await superagent.post(address).type('form').send(payload).redirects(0);
    }

    return response;
  }

Even though all superagent.post calls are preceded by await and wrapped in try/catch blocks, I still get intermittent unhandled errors. Please bear in mind that the this.login() function also calls this same makeRequest one to actually log in to the server in question. The two issues that crop up are:

Error: socket hang up
    at connResetException (node:internal/errors:692:14)
    at Socket.socketOnEnd (node:_http_client:478:23)
    at Socket.emit (node:events:539:35)
    at endReadableNT (node:internal/streams/readable:1345:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

and

Error: Found
    at Request.callback (/usr/lib/node_modules/homebridge-caddx-interlogix/node_modules/superagent/src/node/index.js:900:17)
    at IncomingMessage.<anonymous> (/usr/lib/node_modules/homebridge-caddx-interlogix/node_modules/superagent/src/node/index.js:1153:18)
    at IncomingMessage.emit (node:events:525:35)
    at endReadableNT (node:internal/streams/readable:1358:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

All calls to superagent.post(), as well as to my helper are also prefixed with async and wrapped in try/catch blocks that manage the errors, and these exceptions stay unhandled nonetheless.

Any ideas on this? Thanks in advance :)

vpatil-uptycs commented 1 year ago

@flareman Did you get any solution for this?

flareman commented 1 year ago

I ended up replacing superagent with AXIOS, which worked as expected.