Skip to content

Commit

Permalink
fix(react): Set handled value in ErrorBoundary depending on fallbac…
Browse files Browse the repository at this point in the history
…k [v7] (#11037)

Backport of #10989
  • Loading branch information
Lms24 authored Mar 12, 2024
1 parent c6e03a3 commit 1c71e8e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
4 changes: 3 additions & 1 deletion packages/react/src/errorboundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundarySta
captureContext: {
contexts: { react: { componentStack } },
},
mechanism: { handled: false },
// If users provide a fallback component we can assume they are handling the error.
// Therefore, we set the mechanism depending on the presence of the fallback prop.
mechanism: { handled: !!this.props.fallback },
});

if (onError) {
Expand Down
50 changes: 46 additions & 4 deletions packages/react/test/errorboundary.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ describe('ErrorBoundary', () => {
captureContext: {
contexts: { react: { componentStack: expect.any(String) } },
},
mechanism: { handled: false },
mechanism: { handled: true },
});

expect(mockOnError.mock.calls[0][0]).toEqual(mockCaptureException.mock.calls[0][0]);
Expand Down Expand Up @@ -300,7 +300,7 @@ describe('ErrorBoundary', () => {
captureContext: {
contexts: { react: { componentStack: expect.any(String) } },
},
mechanism: { handled: false },
mechanism: { handled: true },
});

// Check if error.cause -> react component stack
Expand Down Expand Up @@ -339,7 +339,7 @@ describe('ErrorBoundary', () => {
captureContext: {
contexts: { react: { componentStack: expect.any(String) } },
},
mechanism: { handled: false },
mechanism: { handled: true },
});

expect(mockOnError.mock.calls[0][0]).toEqual(mockCaptureException.mock.calls[0][0]);
Expand Down Expand Up @@ -383,7 +383,7 @@ describe('ErrorBoundary', () => {
captureContext: {
contexts: { react: { componentStack: expect.any(String) } },
},
mechanism: { handled: false },
mechanism: { handled: true },
});

expect(mockOnError.mock.calls[0][0]).toEqual(mockCaptureException.mock.calls[0][0]);
Expand Down Expand Up @@ -515,6 +515,48 @@ describe('ErrorBoundary', () => {
expect(mockOnReset).toHaveBeenCalledTimes(1);
expect(mockOnReset).toHaveBeenCalledWith(expect.any(Error), expect.any(String), expect.any(String));
});

it('sets `handled: true` when a fallback is provided', async () => {
render(
<TestApp fallback={({ resetError }) => <button data-testid="reset" onClick={resetError} />}>
<h1>children</h1>
</TestApp>,
);

expect(mockCaptureException).toHaveBeenCalledTimes(0);

const btn = screen.getByTestId('errorBtn');
fireEvent.click(btn);

expect(mockCaptureException).toHaveBeenCalledTimes(1);
expect(mockCaptureException).toHaveBeenLastCalledWith(expect.any(Object), {
captureContext: {
contexts: { react: { componentStack: expect.any(String) } },
},
mechanism: { handled: true },
});
});

it('sets `handled: false` when no fallback is provided', async () => {
render(
<TestApp>
<h1>children</h1>
</TestApp>,
);

expect(mockCaptureException).toHaveBeenCalledTimes(0);

const btn = screen.getByTestId('errorBtn');
fireEvent.click(btn);

expect(mockCaptureException).toHaveBeenCalledTimes(1);
expect(mockCaptureException).toHaveBeenLastCalledWith(expect.any(Object), {
captureContext: {
contexts: { react: { componentStack: expect.any(String) } },
},
mechanism: { handled: false },
});
});
});
});

Expand Down

0 comments on commit 1c71e8e

Please sign in to comment.