Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/react/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export function setCause(error: Error & { cause?: Error }, cause: Error): void {
return;
}
if (error.cause) {
// We can only link to an `Error` cause, not e.g. a string (`new Error('x', { cause: 'y' })`).
if (!isError(error.cause)) {
return;
}
seenErrors.add(error);
return recurse(error.cause, cause);
}
Expand Down
22 changes: 22 additions & 0 deletions packages/react/test/errorboundary.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,28 @@ describe('ErrorBoundary', () => {
expect(cause.message).toEqual(thirdError.message);
});

it('handles when `error.cause` is not an Error', () => {
function CustomBam(): JSX.Element {
const error = new Error('bam');
// @ts-expect-error Need to set cause on error
error.cause = 'timeout';
throw error;
}

const { container } = render(
<TestApp fallback={<p>You have hit an error</p>} errorComp={<CustomBam />}>
<h1>children</h1>
</TestApp>,
);

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

expect(container.innerHTML).toContain('You have hit an error');
expect(mockCaptureException).toHaveBeenCalledTimes(1);
expect(mockCaptureException.mock.calls[0]?.[0].cause).toBe('timeout');
});

it('handles when `error.cause` is recursive', () => {
const mockOnError = vi.fn();

Expand Down
Loading