forwardemail / forwardemail/superagent
Unhandled exceptions occur even when having every superagent request handled
- Dominant language
- JavaScript
- Stars
- 16.6k
- Forks
- 1.3k
- PR merge metrics
- No merged PRs in 30d
Description
I'm using superagent v8.0.0 to manage requests for a homebridge plugin that I have written ([homebridge-caddx-interlogix](https://github.com/flareman/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();
(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. (/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 :)
Contributor guide
Assessment
This issue has not been assessed yet.