Skip to content

Commit e1f97f7

Browse files
authored
fix: fetch('').catch() triggers unhandled rejection in debugger (#15)
1 parent 8724b67 commit e1f97f7

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ function contentLengthFromResponseHeaders(headers: Buffer[]) {
7676
return undefined;
7777
}
7878

79+
async function loadFetch() {
80+
try {
81+
await fetch('');
82+
} catch (_) {
83+
//
84+
}
85+
}
86+
7987
// A combination of https://github.com/elastic/apm-agent-nodejs and
8088
// https://github.com/gadget-inc/opentelemetry-instrumentations/blob/main/packages/opentelemetry-instrumentation-undici/src/index.ts
8189
export class FetchInstrumentation implements Instrumentation {
@@ -110,7 +118,7 @@ export class FetchInstrumentation implements Instrumentation {
110118

111119
constructor(config: FetchInstrumentationConfig) {
112120
// Force load fetch API (since it's lazy loaded in Node 18)
113-
fetch('').catch(() => {});
121+
loadFetch();
114122
this.channelSubs = [];
115123
this.meter = metrics.getMeter(this.instrumentationName, this.instrumentationVersion);
116124
this.tracer = trace.getTracer(this.instrumentationName, this.instrumentationVersion);

0 commit comments

Comments
 (0)