Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NextJS handling of network error #99

Closed
BRB3D opened this issue Jan 14, 2025 · 5 comments
Closed

NextJS handling of network error #99

BRB3D opened this issue Jan 14, 2025 · 5 comments
Assignees
Labels
invalid This doesn't seem right

Comments

@BRB3D
Copy link

BRB3D commented Jan 14, 2025

Describe the feature request

Handling Unleash Network Errors in NextJS Applications

Background

Current Situation

In our project, we wrap the app with the Unleash provider/flagProvider. While this generally works well, there's a critical issue:

  • If there's a network error when fetching from Unleash, the app crashes due to lack of response.

Current Solution

The current approach to handle this is:

  • Create an error wrapper that displays an error screen when a network issue occurs.

Solution suggestions

Proposed Improvement

A more robust solution could involve:

  1. Implementing a fallback mechanism for useFlags() hook:

    • Return false for all flags if there's no connection
    • OR
    • Return a predetermined object of feature flags (some true, some false)
  2. This approach would prevent app crashes and allow for graceful degradation.

Implementation Concept

import { FlagProvider, useFlags } from 'your-unleash-library';

// Fallback flags object
const fallbackFlags = {
  featureA: false,
  featureB: true,
  featureC: false,
  // ... other flags
};

// In your app
const App = () => {
  return (
    <FlagProvider config={/* your config */} fallBack={fallbackFlags}>
      <YourAppComponents />
    </FlagProvider>
  );
};
@BRB3D BRB3D added the enhancement New feature or request label Jan 14, 2025
@FredrikOseberg FredrikOseberg self-assigned this Jan 15, 2025
@FredrikOseberg FredrikOseberg moved this from New to Investigating in Issues and PRs Jan 15, 2025
@Tymek
Copy link
Member

Tymek commented Jan 17, 2025

Can you please provide a reproducible example of an app crashing? We already have a way of providing "fallback" flags (bootstrap config, with <FlagProvider config={{ bootstrap: [...] }} >), and rendering before flags are resolved (const { flagsReady } = useFlagsStatus();).

@Tymek Tymek added invalid This doesn't seem right and removed enhancement New feature or request labels Jan 17, 2025
@BRB3D
Copy link
Author

BRB3D commented Jan 17, 2025

Hello Unleash team,

I apologize for the delayed response. As a new member of the project, I've discovered some important information regarding the recent incident:

  1. Root cause: The app crash was not due to Unleash, as initially thought. Instead, it was caused by an error we throw in a file where we check if unleash is returning flags. So when we had a network error with unleash we threw the error.

  2. Current limitation: We currently don't have a mechanism to detect when these network errors occur although very rear they do happen.

  3. Suggested improvement: It would be beneficial to implement a sort of catch method were it allows to extract hey this is happening because there was a network error?

I appreciate your patience as I've just started on the project and honestly thought it was coming from unleash. Please let me know if you need any further clarification or have any questions.

Thank you for your understanding.

@Tymek Tymek added invalid This doesn't seem right and removed invalid This doesn't seem right labels Jan 20, 2025
@Tymek
Copy link
Member

Tymek commented Jan 20, 2025

Please provide an example causing an app crash. I couldn't reproduce your issue. Network errors are caught in code and printed to the console. There is an option to get errors with listeners (https://docs.getunleash.io/reference/sdks/react#listening-to-events).

@FredrikOseberg
Copy link
Contributor

Hello Unleash team,

I apologize for the delayed response. As a new member of the project, I've discovered some important information regarding the recent incident:

  1. Root cause: The app crash was not due to Unleash, as initially thought. Instead, it was caused by an error we throw in a file where we check if unleash is returning flags. So when we had a network error with unleash we threw the error.
  2. Current limitation: We currently don't have a mechanism to detect when these network errors occur although very rear they do happen.
  3. Suggested improvement: It would be beneficial to implement a sort of catch method were it allows to extract hey this is happening because there was a network error?

I appreciate your patience as I've just started on the project and honestly thought it was coming from unleash. Please let me know if you need any further clarification or have any questions.

Thank you for your understanding.

@BRB3D

The SDKs are designed to not throw errors but instead allow clients to hook into events and listen to errors and decide how to act upon them themselves. Like Tymek said, you can listen on the error events emitted from the SDK and decide what you do with them when you receive an error. I will close this issue for now, but let me know if that works for you.

@github-project-automation github-project-automation bot moved this from Investigating to Done in Issues and PRs Jan 22, 2025
@BRB3D
Copy link
Author

BRB3D commented Jan 22, 2025

A bit more of context, we create the flag provider just like the unleash website. but we have a function that basically evaluatesFlags(getDefinitions, context) if that didnt return the array we threw an error. It usually works, but if there is a network error in unleash then thats why our app was crashing. Now I would love to create a banner that says to users hey some features might not be working. But because its a server-side function I havent found a way to pass it to a client-side Provider. Is there such feature? I mean we will listen when we mount the component but after that not sure?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
Status: Done
Development

No branches or pull requests

3 participants