From e5715c359d7ebac6d90c152843e3ce5f48f22fc2 Mon Sep 17 00:00:00 2001 From: Henry Huck Date: Fri, 10 Jan 2025 12:39:37 +0100 Subject: [PATCH 1/2] feat(react): Add a `handled` prop to ErrorBoundary (#14560) The previous behaviour was to rely on the presence of the `fallback` prop to decide if the error was considered handled or not. The new property lets users explicitly choose what should the handled status be. If omitted, the old behaviour is still applied. --- packages/react/src/errorboundary.tsx | 9 ++- packages/react/test/errorboundary.test.tsx | 82 +++++++++++----------- 2 files changed, 49 insertions(+), 42 deletions(-) diff --git a/packages/react/src/errorboundary.tsx b/packages/react/src/errorboundary.tsx index 91cc0e2cdc17..f500d79466cc 100644 --- a/packages/react/src/errorboundary.tsx +++ b/packages/react/src/errorboundary.tsx @@ -35,6 +35,12 @@ export type ErrorBoundaryProps = { * */ fallback?: React.ReactElement | FallbackRender | undefined; + /** + * If set to `true` or `false`, the error `handled` property will be set to the given value. + * If unset, the default behaviour is to rely on the presence of the `fallback` prop to determine + * if the error was handled or not. + */ + handled?: boolean | undefined; /** Called when the error boundary encounters an error */ onError?: ((error: unknown, componentStack: string | undefined, eventId: string) => void) | undefined; /** Called on componentDidMount() */ @@ -107,7 +113,8 @@ class ErrorBoundary extends React.Component { expect(mockOnReset).toHaveBeenCalledTimes(1); expect(mockOnReset).toHaveBeenCalledWith(expect.any(Error), expect.any(String), expect.any(String)); }); + it.each` + fallback | handled | expected + ${true} | ${undefined} | ${true} + ${false} | ${undefined} | ${false} + ${true} | ${false} | ${false} + ${true} | ${true} | ${true} + ${false} | ${true} | ${true} + ${false} | ${false} | ${false} + `( + 'sets `handled: $expected` when `handled` is $handled and `fallback` is $fallback', + async ({ + fallback, + handled, + expected, + }: { + fallback: boolean; + handled: boolean | undefined; + expected: boolean; + }) => { + const fallbackComponent: FallbackRender | undefined = fallback + ? ({ resetError }) =>