Skip to content

Commit

Permalink
fix: fetch('').catch() triggers unhandled rejection in debugger (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish authored Jun 7, 2024
1 parent 8724b67 commit e1f97f7
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ function contentLengthFromResponseHeaders(headers: Buffer[]) {
return undefined;
}

async function loadFetch() {
try {
await fetch('');
} catch (_) {
//
}
}

// A combination of https://github.com/elastic/apm-agent-nodejs and
// https://github.com/gadget-inc/opentelemetry-instrumentations/blob/main/packages/opentelemetry-instrumentation-undici/src/index.ts
export class FetchInstrumentation implements Instrumentation {
Expand Down Expand Up @@ -110,7 +118,7 @@ export class FetchInstrumentation implements Instrumentation {

constructor(config: FetchInstrumentationConfig) {
// Force load fetch API (since it's lazy loaded in Node 18)
fetch('').catch(() => {});
loadFetch();
this.channelSubs = [];
this.meter = metrics.getMeter(this.instrumentationName, this.instrumentationVersion);
this.tracer = trace.getTracer(this.instrumentationName, this.instrumentationVersion);
Expand Down

0 comments on commit e1f97f7

Please sign in to comment.