Skip to content

Error boundary does not reset state #6755

Closed

Description

@sorenlouv

Codesandbox: https://pu4n7z.csb.app/
Codesandbox source: https://codesandbox.io/s/eui-error-boundary-problem-pu4n7z

After the error boundary catches an exception, it is no longer possible to reset its state. It should be possible to reset the state on demand, like when navigating to another page.

error boundary

Activity

  1. added
    ⚠️ needs validation
    on May 8, 2023
  2. 1Copenut commented on May 8, 2023

    @1Copenut
    Contributor

    Hi @sqren. I did some research and was able to solve your issue by adding a const errKey = page; and passing that unique key to <EuiErrorBoundary key={errKey}>. I forked your CodeSandbox to demonstrate the proposed solution: https://codesandbox.io/s/eui-error-boundary-problem-forked-ivxj49?file=/my_template.jsx

  3. added
    answered
    and removed
    ⚠️ needs validation
    on May 8, 2023
  4. sorenlouv commented on May 8, 2023

    @sorenlouv
    ContributorAuthor

    Hi @1Copenut,

    Thanks for taking a look! The problem with adding a key property is that it will unmount an remount the error boundary and everything below. All Observability apps uses the same template which has an error boundary wrapped around the content. This means the entire application will be remounted on every route change. This has pretty severe impact on performance and was exactly what was happening until I made this fix.

    I'm not sure about the right solution. Maybe something like <EuiErrorBoundary conditional-key={errKey}> where it only remounts if conditional-key changes and hasError: true:

    interface EuiErrorBoundaryState {
    hasError: boolean;
    error?: string;
    }

  5. cee-chen commented on May 8, 2023

    @cee-chen
    Contributor

    Trevor's approach is a good workaround (using key to force a re-mount is relatively common in React), but I want to also address this:

    It should be possible to reset the state on demand, like when navigating to another page.

    EuiErrorBoundary is (currently) not intended to be a super flexible or complex component - it's meant for very basic try/catch usage where its loading is, e.g. when a conditional wrapper, or its presence forces a hard page reload.

    That being said, while EUI as team would not prioritize any work on changing it, we'd be open to a community contribution that improves consumer control & flexibility over it (e.g. a prop that allows passing an error directly to it or exposes an onError callback, e.g. #6127 as well). If you're interested in helping extend the component, let us know! Otherwise, I'd recommend using a conditional wrapper or a key remount workaround.

  6. added
    help wanted
    and removed on May 8, 2023
  7. github-actions commented on May 8, 2023

    @github-actions

    👋 Thank you for your suggestion or request! While the EUI team agrees that it's valid, it's unlikely that we will prioritize this issue on our roadmap. We'll leave the issue open if you or anyone else in the community wants to implement it by contributing to EUI. If not, this issue will auto close in one year.

  8. cee-chen commented on May 8, 2023

    @cee-chen
    Contributor

    All Observability apps uses the same template which has an error boundary wrapped around the content. This means the entire application will be remounted on every route change. This has pretty severe impact on performance

    Oh wow, just saw this. I... don't know that we really intended EuiErrorBoundary to be a wrapper for an entire page template 😬 Maybe like a subsection of a page or a flyout or around a code block, but it feels extremely heavy-handed for an entire page where a lot of things can go wrong.

    We need to figure out what the goal of Obs' error boundary is before we can recommend an approach. In general I don't know if I'd recommend a whole-template error boundary.

  9. removed
    answered
    on May 8, 2023
  10. sorenlouv commented on May 8, 2023

    @sorenlouv
    ContributorAuthor

    Oh wow, just saw this. I... don't know that we really intended EuiErrorBoundary to be a wrapper for an entire page template 😬 Maybe like a subsection of a page or a flyout or around a code block, but it feels extremely heavy-handed for an entire page where a lot of things can go wrong.

    I can ask the team with the Observability Shared UX team to remove the error boundary from the template. This will mean that Observability apps will no longer have an error boundary (unless they implement one themselves). I'm not necessarily against it. Might be better that way.

  11. cee-chen commented on May 8, 2023

    @cee-chen
    Contributor

    I think the other option is to not add a key remount (which was the change you made) and simply force the user to refresh the page to "resolve" or retry the error. In theory a thrown error means the entire app has gone awry and a reload is often the "correct"/easiest way to handle app-wide thrown errors, rather than the entire page hanging or going blank.

    @tkajtoch @ryankeairns y'all had some thoughts on this at today's sync - any thoughts on general recommendations we can make for last resort error boundaries for entire pages? Is "don't do it" the best path forward? 😅

  12. sorenlouv commented on May 24, 2023

    @sorenlouv
    ContributorAuthor

    @cee-chen Another problem I've noticed with the error boundary is that it catches the exception but doesn't log an error. This makes it very hard to figure out where the error occurred - normally you can click on an error in the browser's Dev Tools to navigate to the exact line of where it occurred.

    image
  13. cee-chen commented on May 25, 2023

    @cee-chen
    Contributor

    @sqren That's a fair remark, but as far as I can tell that's the intended behavior of EuiErrorBoundary. It's meant to serve as the catch in try/catch and surface the error visually rather than in the console.

    If you're open to contributing back to EUI, we'd certainly accept a new prop such as rethrowError or logError which re-throws or console.error()s the error if that's your desired behavior, but we probably wouldn't make it the default behavior.

    Another (probably more flexible!) option would be for us to accept an onError callback where we pass the error back to the developer, whereupon you can then re-throw it or log the error to the console however you please. #6127

    It looks like I've referenced that issue twice now as a viable solution to several issues you've mentioned in this thread. Honestly, I'm probably just going to move forward with that prop. It really feels like the most flexible solution.

  14. cee-chen commented on May 25, 2023

    @cee-chen
    Contributor

    @sqren FYI I'll be adding onError support anyway (it's easy to do and long overdue), but are you sure console errors aren't being thrown or that something else in Kibana isn't swallowing the error? I'm seeing https://elastic.github.io/eui/#/utilities/error-boundary throw an error in my console on latest Firefox.

  15. sorenlouv commented on May 25, 2023

    @sorenlouv
    ContributorAuthor

    @cee-chen Thank you so much!

    but are you sure console errors aren't being thrown or that something else in Kibana isn't swallowing the error? I'm seeing https://elastic.github.io/eui/#/utilities/error-boundary throw an error in my console on latest Firefox.

    Hmm.. you may have a point. There could be other types of error boundaries (not the EUI one) at play. I'll take a look. Thanks again!

  16. self-assigned this
    on May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

help wanted

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions