From 834e1427ecd7d4a4a71a18039a7c85edda6bb076 Mon Sep 17 00:00:00 2001 From: HHK1 Date: Tue, 3 Dec 2024 17:31:15 +0100 Subject: [PATCH 1/3] feat(react): add a `handled` prop to ErrorBoundary 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 the consumer explicitely 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 fbc17f94c378..0c21db41e34e 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.only.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 }) =>