Skip to content

Commit ebdfc74

Browse files
vsandvolddigitalsadhu
authored andcommitted
fix: use AbortController instead of AbortSignal to avoid unhandled exception (#412)
With this PR, we replace `AbortSignal.timeout()` with the more general `AbortController.abort()` in combination with `setTimeout`. This change prevents an unhandled exception propagating up outside the scope of the calling function. Fixes #411
1 parent 27927e3 commit ebdfc74

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

lib/http.js

+18-8
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,23 @@ export default class HTTP {
2222
* @returns {Promise<Pick<import('undici').Dispatcher.ResponseData, 'statusCode' | 'headers' | 'body'>>}
2323
*/
2424
async request(url, options) {
25-
const { statusCode, headers, body } = await this.requestFn(
26-
new URL(url),
27-
{
28-
...options,
29-
signal: AbortSignal.timeout(options.timeout || 1000),
30-
},
31-
);
32-
return { statusCode, headers, body };
25+
const abortController = new AbortController();
26+
27+
const timeoutId = setTimeout(() => {
28+
abortController.abort();
29+
}, options.timeout || 1000);
30+
31+
try {
32+
const { statusCode, headers, body } = await this.requestFn(
33+
new URL(url),
34+
{
35+
...options,
36+
signal: abortController.signal,
37+
},
38+
);
39+
return { statusCode, headers, body };
40+
} finally {
41+
clearTimeout(timeoutId);
42+
}
3343
}
3444
}

0 commit comments

Comments
 (0)