Skip to content

feat(core) : Truncate the cause message to a maxLengthValue of 250 #8592

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

Closed
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
6 changes: 4 additions & 2 deletions packages/react/src/errorboundary.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ReportDialogOptions, Scope } from '@sentry/browser';
import { captureException, getCurrentHub, showReportDialog, withScope } from '@sentry/browser';
import { isError, logger } from '@sentry/utils';
import { isError, logger, truncate } from '@sentry/utils';
import hoistNonReactStatics from 'hoist-non-react-statics';
import * as React from 'react';

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

export const UNKNOWN_COMPONENT = 'unknown';
const MAX_VALUE_LENGTH = 250;

export type FallbackRender = (errorData: {
error: Error;
Expand Down Expand Up @@ -66,7 +67,7 @@ const INITIAL_STATE = {
eventId: null,
};

function setCause(error: Error & { cause?: Error }, cause: Error): void {
export function setCause(error: Error & { cause?: Error }, cause: Error): void {
const seenErrors = new WeakMap<Error, boolean>();

function recurse(error: Error & { cause?: Error }, cause: Error): void {
Expand All @@ -79,6 +80,7 @@ function setCause(error: Error & { cause?: Error }, cause: Error): void {
seenErrors.set(error, true);
return recurse(error.cause, cause);
}
cause.message = truncate(cause.message, MAX_VALUE_LENGTH);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No action required at this time, see my other comment but:

The maxValueLength can be configured in the SDK's init options. We should respect that and only fall back to MAX_VALUE_LENGTH if the option is not set.

error.cause = cause;
}

Expand Down
38 changes: 37 additions & 1 deletion packages/react/test/errorboundary.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as React from 'react';
import { useState } from 'react';

import type { ErrorBoundaryProps } from '../src/errorboundary';
import { ErrorBoundary, isAtLeastReact17, UNKNOWN_COMPONENT, withErrorBoundary } from '../src/errorboundary';
import { ErrorBoundary, isAtLeastReact17, setCause, UNKNOWN_COMPONENT, withErrorBoundary } from '../src/errorboundary';

const mockCaptureException = jest.fn();
const mockShowReportDialog = jest.fn();
Expand Down Expand Up @@ -382,6 +382,42 @@ describe('ErrorBoundary', () => {
expect(cause.name).not.toContain('React ErrorBoundary');
});

it('should truncate cause.message to maxLengthValue = 250 ', () => {
const mockOnError = jest.fn();

function CustomBam(): JSX.Element {
const error = new Error('Error example');
// The cause message with 620 characters
const cause = new Error('This is a very long cause message that exceeds 250 characters '.repeat(10));
setCause(error, cause);
throw error;
}

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

expect(mockOnError).toHaveBeenCalledTimes(0);
expect(mockCaptureException).toHaveBeenCalledTimes(0);

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

expect(mockCaptureException).toHaveBeenCalledTimes(1);
expect(mockCaptureException).toHaveBeenLastCalledWith(expect.any(Error), {
contexts: { react: { componentStack: expect.any(String) } },
});

expect(mockOnError.mock.calls[0][0]).toEqual(mockCaptureException.mock.calls[0][0]);

const error = mockCaptureException.mock.calls[0][0];
const cause = error.cause;
// We need to make sure that the length of the cause message is 253 (3 characters of etc sign (...) in the truncate function)
expect(cause.message).toHaveLength(253);
});

it('calls `beforeCapture()` when an error occurs', () => {
const mockBeforeCapture = jest.fn();

Expand Down