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

fix(react): Set handled value in ErrorBoundary depending on fallback [v7] #11037

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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