Flagsmith / flagsmith-js-client

Javascript Client for Flagsmith. Ship features with confidence using feature flags and remote config. Host yourself or use our hosted version at https://www.flagsmith.com/

Home Page:https://www.flagsmith.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

React: Unhandled Rejection (TypeError): PromiseReject called on non-object

Frug opened this issue · comments

When initializing the client in a react project I'm unable to gracefully catch/handle the error if a connection the the API endpoint fails.

To reproduce this error using your example script, try adding the api parameter to init, using some unresponsive URL. ie:

componentWillMount() {
     const { handleFlags, handleFlagsError } = this;
     flagsmith.init({
       api: 'http://notavalidurl.com',
       environmentID,
       ...
    });
...

The unhnadled rejection bubbles to the top and I can't seem to catch it. I've tried adding the onError callback handler to init, as well as adding a .catch() after the init call, but nothing wants to catch this exception.

I don't know if this is related, but I get PromiseResolve called on non-object in the onError callback. I initialize flagsmith outside of react.

flagsmith.init({
  environmentID: process.env.REACT_APP_FLAGSMITH_ENVIRONMENT_KEY,
  cacheFlags: true, // stores flags in localStorage cache
  enableAnalytics: true, // See https://docs.flagsmith.com/flag-analytics/ for more info
  onChange: async (oldFlags, params) => {
    const event = new CustomEvent("customevent", {
      detail: {
        oldFlags,
        params,
      },
    });

    window.dispatchEvent(event);
  },
  onError: (e) => {
    console.log("error", e);
  },
});

Resolved in native-flagsmith@1.7.4 and flagsmith@1.7.4 🚀 Via #99