-
Notifications
You must be signed in to change notification settings - Fork 97
feat(react): Convert non-Error objects to Error instances in ErrorBoundary #1786
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
eecd9a2
e381c6d
7e6204f
a779664
109a5c0
7f72a40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| {"root":["./src/App.tsx","./src/main.tsx","./src/vite-env.d.ts","./vite.config.ts"],"version":"5.8.3"} | ||
| {"root":["./src/App.tsx","./src/main.tsx","./src/vite-env.d.ts","./vite.config.ts"],"version":"5.9.2"} | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -128,12 +128,20 @@ const initialErrorBoundaryState: ErrorBoundaryState = { | |||||||
| error: null, | ||||||||
| } | ||||||||
|
|
||||||||
| // Although `componentDidCatch` and `getDerivedStateFromError` are typed to accept an `Error` object, | ||||||||
| // they can also be invoked with non-error objects. This is why we need to convert them to Error instances. | ||||||||
| // See: https://github.com/getsentry/sentry-javascript/issues/6167 | ||||||||
| const convertToError = (error: unknown): Error => { | ||||||||
| return error instanceof Error ? error : new Error(String(error)) | ||||||||
| } | ||||||||
|
|
||||||||
| class BaseErrorBoundary<TShouldCatch extends ShouldCatch> extends Component< | ||||||||
| ErrorBoundaryProps<TShouldCatch>, | ||||||||
| ErrorBoundaryState | ||||||||
| > { | ||||||||
| static getDerivedStateFromError(error: Error): ErrorBoundaryState { | ||||||||
| return { isError: true, error } | ||||||||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||||||||
| static getDerivedStateFromError(error: any): ErrorBoundaryState { | ||||||||
| return { isError: true, error: convertToError(error) } | ||||||||
| } | ||||||||
|
Comment on lines
+170
to
173
|
||||||||
|
|
||||||||
| state = initialErrorBoundaryState | ||||||||
|
|
@@ -146,8 +154,9 @@ class BaseErrorBoundary<TShouldCatch extends ShouldCatch> extends Component< | |||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| componentDidCatch(error: InferError<TShouldCatch>, info: ErrorInfo) { | ||||||||
| this.props.onError?.(error, info) | ||||||||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||||||||
| componentDidCatch(error: any, info: ErrorInfo) { | ||||||||
|
Comment on lines
+185
to
+186
|
||||||||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | |
| componentDidCatch(error: any, info: ErrorInfo) { | |
| componentDidCatch(error: unknown, info: ErrorInfo) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tsconfig.tsbuildinfoappears to be a generated build artifact, and this change is only updating the embedded TypeScript version (5.8.3 → 5.9.2) without any related source changes. It should typically be reverted/removed from the PR to avoid noisy diffs tied to local tooling versions.