PostHog / posthog-js-lite

Reimplementation of posthog-js to be as light and modular as possible.
https://posthog.com/docs/libraries
MIT License
70 stars 36 forks source link

Bug: posthog.on('error') always catches http fetch error #180

Closed L03TJ3 closed 9 months ago

L03TJ3 commented 9 months ago

Bug description

We added a logger to catch whenever posthog failed to load at all. It seems posthog.on('error') should be used for this. There is an initial HTTP fetch error always logged and catched, even if the calls to host are succesfull.

How to reproduce

  1. Not sure. pretty straigh-forward implementation where we only use getFeatureFlag(s)
  2. when relying on the default where sendFeatureFlagEvent is enabled, the .on('error') always triggers after default retry's and timeouts

Related sub-libraries

Additional context

  1. we use a reverse proxy
  2. it seems to be triggered once sendFeatureFlagEvent is enabled. disabling this and I don't catch a fetch error (which it is by default)

Thank you for your bug report – we love squashing them!

sirpy commented 9 months ago

Also i've noticed that the fetch error is:

  1. displayed with error code 0 (ie not an http error code)
  2. I've noticed it happens when moving to another tab and going back
  3. it doesnt seem to generate any network calls
marandaneto commented 9 months ago

Hi @sirpy thanks for reporting this, let me check it.

marandaneto commented 9 months ago

@sirpy or @L03TJ3 can you enable debug and check the console logs? what is the error about? is there any more info either before or after the error? are you using PostHog self-hosted or the EU or US cloud? what is your api_host?

Do first thing after the SDK is init: posthog.debug(false)

I cannot reproduce it locally so I'm not sure what it can be.

sirpy commented 9 months ago

@marandaneto We are using your service I believe US cloud. we use vercelrp.gooddollar.org as a reverse proxy to your api host. Also notice that we are using the posthog react native in browser env.

marandaneto commented 9 months ago
Screenshot 2024-02-15 at 11 35 23

I can reproduce it using RN Web, will check it out, thanks.

marandaneto commented 9 months ago

It's not really because of sendFeatureFlagEvent, if you call posthog.capture('test'); you can reproduce that already, it just happens that sendFeatureFlagEvent=true captures an event when feature flags are used.

marandaneto commented 9 months ago

If you init the SDK with captureMode: 'json', this does not happen either, but its form by default.

marandaneto commented 9 months ago

Removing this, the error stops. Using this flag as defined here, the status is always 0, so the error will be thrown here.

marandaneto commented 9 months ago

https://github.com/PostHog/posthog-js-lite/releases/tag/posthog-react-native-v2.11.4