Skip to content

Commit 5a14a38

Browse files
authored
feat(ui): Add error.cause to <ErrorHandler> (#90749)
This PR fixes `<ErrorHandler>` to add a `cause` error to when we catch an error with a React Error Boundary. This will help us debug [this error](https://sentry.sentry.io/issues/6333431167/events/ecf1545d92834ceba543c2b62af63171/?project=11276) (and future ones that hit `<ErrorHandler>`). See [this issue](https://sentry-emerging-tech.sentry.io/issues/6580362564/?project=6326737&query=is%3Aunresolved%20issue.priority%3A%5Bhigh%2C%20medium%5D&referrer=issue-stream&stream_index=0) for what it will look like.
1 parent 05139d1 commit 5a14a38

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

static/app/components/errorBoundary.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,18 @@ class ErrorBoundary extends Component<Props, State> {
5555
Object.keys(errorTag).forEach(tag => scope.setTag(tag, errorTag[tag]));
5656
}
5757

58-
// Based on https://github.com/getsentry/sentry-javascript/blob/6f4ad562c469f546f1098136b65583309d03487b/packages/react/src/errorboundary.tsx#L75-L85
59-
const errorBoundaryError = new Error(error.message);
60-
errorBoundaryError.name = `React ErrorBoundary ${errorBoundaryError.name}`;
61-
errorBoundaryError.stack = errorInfo.componentStack!;
62-
63-
error.cause = errorBoundaryError;
64-
65-
scope.setExtra('errorInfo', errorInfo);
66-
Sentry.captureException(error);
58+
try {
59+
// Based on https://github.com/getsentry/sentry-javascript/blob/6f4ad562c469f546f1098136b65583309d03487b/packages/react/src/errorboundary.tsx#L75-L85
60+
const errorBoundaryError = new Error(error.message);
61+
errorBoundaryError.name = `React ErrorBoundary ${errorBoundaryError.name}`;
62+
errorBoundaryError.stack = errorInfo.componentStack!;
63+
error.cause = errorBoundaryError;
64+
} catch {
65+
// Some browsers won't let you write to Error instance
66+
scope.setExtra('errorInfo', errorInfo);
67+
} finally {
68+
Sentry.captureException(error);
69+
}
6770
});
6871
}
6972

static/app/components/lazyLoad.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,20 @@ class ErrorBoundary extends Component<{children: React.ReactNode}, ErrorBoundary
7676
if (isWebpackChunkLoadingError(error)) {
7777
scope.setFingerprint(['webpack', 'error loading chunk']);
7878
}
79-
scope.setExtra('errorInfo', errorInfo);
80-
Sentry.captureException(error);
79+
try {
80+
// Based on https://github.com/getsentry/sentry-javascript/blob/6f4ad562c469f546f1098136b65583309d03487b/packages/react/src/errorboundary.tsx#L75-L85
81+
const errorBoundaryError = new Error(error.message);
82+
errorBoundaryError.name = `React ErrorBoundary ${errorBoundaryError.name}`;
83+
errorBoundaryError.stack = errorInfo.componentStack!;
84+
85+
// This will mutate `error` and get captured to Sentry in `RouteError`
86+
error.cause = errorBoundaryError;
87+
} catch {
88+
// Some browsers won't let you write to Error instance
89+
scope.setExtra('errorInfo', errorInfo);
90+
} finally {
91+
Sentry.captureException(error);
92+
}
8193
});
8294

8395
// eslint-disable-next-line no-console

static/app/utils/errorHandler.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,24 @@ export default function errorHandler<P>(WrappedComponent: React.ComponentType<P>
2424
error: undefined,
2525
};
2626

27-
componentDidCatch(_error: Error, info: React.ErrorInfo) {
27+
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
2828
// eslint-disable-next-line no-console
2929
console.error(
3030
'Component stack trace caught in <ErrorHandler />:',
31-
info.componentStack
31+
errorInfo.componentStack
3232
);
33+
34+
try {
35+
// Based on https://github.com/getsentry/sentry-javascript/blob/6f4ad562c469f546f1098136b65583309d03487b/packages/react/src/errorboundary.tsx#L75-L85
36+
const errorBoundaryError = new Error(error.message);
37+
errorBoundaryError.name = `React ErrorBoundary ${errorBoundaryError.name}`;
38+
errorBoundaryError.stack = errorInfo.componentStack!;
39+
40+
// This will mutate `error` and get captured to Sentry in `RouteError`
41+
error.cause = errorBoundaryError;
42+
} catch {
43+
// Some browsers won't let you write to Error instance
44+
}
3345
}
3446

3547
render() {

0 commit comments

Comments
 (0)