nuxt-modules / plausible

🎟️ Plausible Analytics integration for Nuxt
https://plausible.io
MIT License
107 stars 7 forks source link

How to capture fetch errors? #28

Closed toniengelhardt closed 4 months ago

toniengelhardt commented 4 months ago

I'm getting a lot of TypeError: Failed to fetch in Sentry from failing Plausible fetch requests:

Screenshot 2024-07-11 at 22 43 22

I tried wrapping the useTrackEvent composable into a try/catch like so:

Screenshot 2024-07-11 at 22 42 52

But the events still get logged and spam Sentry.

Not sure why exactly the requests to the Plausible API fail, but is there a way to properly catch/handle/ignore them?

Might be an upstream issue in plausible-tracker?

johannschopplich commented 4 months ago

We're not using the Plausible tracker package anymore, since it has been abandoned for years. Instead, we use a fork by @Barbapapazes. Maybe he can tell use more why wrapping the composable won't catch a failing fetch promise? 👀

Barbapapazes commented 4 months ago

Hello 👋,

It's related to this function https://github.com/Barbapapazes/plausible-tracker/blob/main/src/event.ts#L72

Do you think I should add a .catch and let the dev passing a callback to handle it?

Barbapapazes commented 4 months ago

And @toniengelhardt, I think the try...catch does not work because I do not await the event

Barbapapazes commented 4 months ago

Do you know if every request fails or if it is only some of them (like someone using a blocker) or does Plausible change something to their API?

toniengelhardt commented 4 months ago

@johannschopplich @Barbapapazes thanks for investigating 🙏🏽

Only very few events fail – not sure if bc of a blocker or bc the Plausible API drops them for some reason – but with a couple thousand events this still leads to quite some noise in Sentry (and unfortunately fetch errors are hard to decipher there).

For me, awaiting the event would make a lot of sense, so that wrapping the composable like in the example would work. But it might be also useful to be able to pass an error handler, ideally globally in the config and directly in the useTrackEvent composable in case it is only needed occasionally.

A failSilently flag would also work, but is probably the most limiting solution.

Any of the above would work for my use case as long as I can somehow prevent errors from being thrown.

Barbapapazes commented 4 months ago

I will need to tackle this in my fork because there trackEvent does not return a promise.

Barbapapazes commented 4 months ago

related to https://github.com/Barbapapazes/plausible-tracker/issues/11

Barbapapazes commented 4 months ago

@toniengelhardt I think you can use the patch (do no hesitate to ping me if it's not working for you) in the issue above because I'll need to add tests to ensure the quality and the future of this fork (and it could takes some times)

toniengelhardt commented 4 months ago

Thanks @Barbapapazes 🙏🏽

Barbapapazes commented 4 months ago

You can follow the progression with this PR: https://github.com/Barbapapazes/plausible-tracker/pull/13

Barbapapazes commented 4 months ago

Test have been merged, I can now fix this issue.

johannschopplich commented 4 months ago

This should be resolved with the latest version v1.0.1, which uses the latest version of @Barbapapazes's tracker library. Thanks for the updates!