Skip to content

Commit

Permalink
catch and swallow exceptions on fetch
Browse files Browse the repository at this point in the history
while sending logs exceptions could happend, swallowing the exception
ensures we don't interrupts customer apps or break api functions
responses. We do the same in web-vitals.
  • Loading branch information
schehata committed Jan 10, 2023
1 parent f1a84b1 commit e2e107c
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,23 @@ export class Logger {
}
const reqOptions: RequestInit = { body, method, keepalive, headers };

function sendFallback() {
// Do not leak network errors; does not affect the running app
fetch(url, reqOptions).catch(console.error);
}

try {
if (typeof fetch === 'undefined') {
const fetch = await require('whatwg-fetch');
await fetch(url, reqOptions);
fetch(url, reqOptions).catch(console.error);
} else if (config.isBrowser && isVercel && navigator.sendBeacon) {
// sendBeacon fails if message size is greater than 64kb, so
// we fall back to fetch.
if (!navigator.sendBeacon(url, body)) {
await fetch(url, reqOptions);
sendFallback()
}
} else {
await fetch(url, reqOptions);
sendFallback()
}
} catch (e) {
console.error(`Failed to send logs to Axiom: ${e}`);
Expand Down

0 comments on commit e2e107c

Please sign in to comment.