Skip to content

Commit

Permalink
Merge pull request #103 from axiomhq/catch-flush-errors-on-logs
Browse files Browse the repository at this point in the history
catch and swallow exceptions on fetch
  • Loading branch information
schehata committed Jan 31, 2023
2 parents 28fd9dd + 2ea6ff4 commit 8f274f1
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

1 comment on commit 8f274f1

@vercel
Copy link

@vercel vercel bot commented on 8f274f1 Jan 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

next-axiom-example – ./

next-axiom-example-lemon.vercel.app
next-axiom-example.axiom.dev
next-axiom-example-git-main.axiom.dev

Please sign in to comment.