Skip to content

Commit 2c37943

Browse files
truncate the cause message to 250 characters
1 parent 1c28688 commit 2c37943

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

packages/react/src/errorboundary.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ReportDialogOptions, Scope } from '@sentry/browser';
22
import { captureException, getCurrentHub, showReportDialog, withScope } from '@sentry/browser';
3-
import { isError, logger } from '@sentry/utils';
3+
import { isError, logger, truncate } from '@sentry/utils';
44
import hoistNonReactStatics from 'hoist-non-react-statics';
55
import * as React from 'react';
66

@@ -10,6 +10,7 @@ export function isAtLeastReact17(version: string): boolean {
1010
}
1111

1212
export const UNKNOWN_COMPONENT = 'unknown';
13+
const MAX_VALUE_LENGTH = 250;
1314

1415
export type FallbackRender = (errorData: {
1516
error: Error;
@@ -79,6 +80,7 @@ function setCause(error: Error & { cause?: Error }, cause: Error): void {
7980
seenErrors.set(error, true);
8081
return recurse(error.cause, cause);
8182
}
83+
cause.message = truncate(cause.message, MAX_VALUE_LENGTH);
8284
error.cause = cause;
8385
}
8486

packages/react/test/errorboundary.test.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,43 @@ describe('ErrorBoundary', () => {
382382
expect(cause.name).not.toContain('React ErrorBoundary');
383383
});
384384

385+
it('should truncate cause.message to 250 characters', () => {
386+
const mockOnError = jest.fn();
387+
388+
function CustomBam(): JSX.Element {
389+
const error = new Error('bam');
390+
// The cause message with 610 characters
391+
const cause = new Error('This is a very long cause message that exceeds 250 characters '.repeat(10));
392+
// @ts-ignore Need to set cause on error
393+
error.cause = cause;
394+
throw error;
395+
}
396+
397+
render(
398+
<TestApp fallback={<p>You have hit an error</p>} onError={mockOnError} errorComp={<CustomBam />}>
399+
<h1>children</h1>
400+
</TestApp>,
401+
);
402+
403+
expect(mockOnError).toHaveBeenCalledTimes(0);
404+
expect(mockCaptureException).toHaveBeenCalledTimes(0);
405+
406+
const btn = screen.getByTestId('errorBtn');
407+
fireEvent.click(btn);
408+
409+
expect(mockCaptureException).toHaveBeenCalledTimes(1);
410+
expect(mockCaptureException).toHaveBeenLastCalledWith(expect.any(Error), {
411+
contexts: { react: { componentStack: expect.any(String) } },
412+
});
413+
414+
expect(mockOnError.mock.calls[0][0]).toEqual(mockCaptureException.mock.calls[0][0]);
415+
416+
const error = mockCaptureException.mock.calls[0][0];
417+
const cause = error.cause;
418+
// We need to make sure that the length of the cause message is 250
419+
expect(cause.message).toHaveLength(250);
420+
});
421+
385422
it('calls `beforeCapture()` when an error occurs', () => {
386423
const mockBeforeCapture = jest.fn();
387424

0 commit comments

Comments
 (0)