Skip to content
Draft
Show file tree
Hide file tree
Changes from 5 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
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"}
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

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

tsconfig.tsbuildinfo appears 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.

Suggested change
{"root":["./src/App.tsx","./src/main.tsx","./src/vite-env.d.ts","./vite.config.ts"],"version":"5.9.2"}
{"root":["./src/App.tsx","./src/main.tsx","./src/vite-env.d.ts","./vite.config.ts"],"version":"5.8.3"}

Copilot uses AI. Check for mistakes.
151 changes: 150 additions & 1 deletion packages/react/src/ErrorBoundary.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, render, screen } from '@testing-library/react'
import { userEvent } from '@testing-library/user-event'
import { type ComponentRef, createElement, createRef } from 'react'
import { type ComponentRef, type ReactNode, createElement, createRef } from 'react'
import { ErrorBoundary as ReactErrorBoundary } from 'react-error-boundary'
import {
ErrorBoundary,
Expand Down Expand Up @@ -84,6 +84,155 @@ describe('<ErrorBoundary/>', () => {
expect(onError).toHaveBeenCalledTimes(1)
})

it('should convert null to Error instance', () => {
const fallbackFn = vi.fn<(props: ErrorBoundaryFallbackProps) => ReactNode>()
fallbackFn.mockImplementation(({ error }) => <div>{error.message}</div>)

render(
<ErrorBoundary fallback={fallbackFn}>
{createElement(() => {
// eslint-disable-next-line @typescript-eslint/only-throw-error
throw null
})}
</ErrorBoundary>
)

expect(fallbackFn).toHaveBeenCalled()
const error = fallbackFn.mock.calls[0][0].error
expect(error).toBeInstanceOf(Error)
expect(error.message).toBe('null')
})

it('should convert string to Error instance', () => {
const fallbackFn = vi.fn<(props: ErrorBoundaryFallbackProps) => ReactNode>()
fallbackFn.mockImplementation(({ error }) => <div>{error.message}</div>)

render(
<ErrorBoundary fallback={fallbackFn}>
{createElement(() => {
// eslint-disable-next-line @typescript-eslint/only-throw-error
throw 'string error'
})}
</ErrorBoundary>
)

expect(fallbackFn).toHaveBeenCalled()
const error = fallbackFn.mock.calls[0][0].error
expect(error).toBeInstanceOf(Error)
expect(error.message).toBe('string error')
})

it('should convert number to Error instance', () => {
const fallbackFn = vi.fn<(props: ErrorBoundaryFallbackProps) => ReactNode>()
fallbackFn.mockImplementation(({ error }) => <div>{error.message}</div>)

render(
<ErrorBoundary fallback={fallbackFn}>
{createElement(() => {
// eslint-disable-next-line @typescript-eslint/only-throw-error
throw 42
})}
</ErrorBoundary>
)

expect(fallbackFn).toHaveBeenCalled()
const error = fallbackFn.mock.calls[0][0].error
expect(error).toBeInstanceOf(Error)
expect(error.message).toBe('42')
})

it('should convert plain object to Error instance', () => {
const fallbackFn = vi.fn<(props: ErrorBoundaryFallbackProps) => ReactNode>()
fallbackFn.mockImplementation(({ error }) => <div>{error.message}</div>)

render(
<ErrorBoundary fallback={fallbackFn}>
{createElement(() => {
// eslint-disable-next-line @typescript-eslint/only-throw-error
throw { message: 'object error' }
})}
</ErrorBoundary>
)

expect(fallbackFn).toHaveBeenCalled()
const error = fallbackFn.mock.calls[0][0].error
expect(error).toBeInstanceOf(Error)
expect(error.message).toBe('[object Object]')
})

it('should keep Error instance as-is when Error is thrown', () => {
const fallbackFn = vi.fn<(props: ErrorBoundaryFallbackProps) => ReactNode>()
fallbackFn.mockImplementation(({ error }) => <div>{error.message}</div>)
const originalError = new Error('original error')

render(
<ErrorBoundary fallback={fallbackFn}>
{createElement(() => {
throw originalError
})}
</ErrorBoundary>
)

expect(fallbackFn).toHaveBeenCalled()
const error = fallbackFn.mock.calls[0][0].error
expect(error).toBeInstanceOf(Error)
expect(error).toBe(originalError)
})

it('should pass Error instance to onError callback when null is thrown', () => {
const onError = vi.fn()

render(
<ErrorBoundary fallback={<div>fallback</div>} onError={onError}>
{createElement(() => {
// eslint-disable-next-line @typescript-eslint/only-throw-error
throw null
})}
</ErrorBoundary>
)

expect(onError).toHaveBeenCalled()
const error = onError.mock.calls[0][0]
expect(error).toBeInstanceOf(Error)
expect(error.message).toBe('null')
})

it('should pass Error instance to onError callback when string is thrown', () => {
const onError = vi.fn()

render(
<ErrorBoundary fallback={<div>fallback</div>} onError={onError}>
{createElement(() => {
// eslint-disable-next-line @typescript-eslint/only-throw-error
throw 'string error'
})}
</ErrorBoundary>
)

expect(onError).toHaveBeenCalled()
const error = onError.mock.calls[0][0]
expect(error).toBeInstanceOf(Error)
expect(error.message).toBe('string error')
})

it('should pass Error instance to onError callback when number is thrown', () => {
const onError = vi.fn()

render(
<ErrorBoundary fallback={<div>fallback</div>} onError={onError}>
{createElement(() => {
// eslint-disable-next-line @typescript-eslint/only-throw-error
throw 42
})}
</ErrorBoundary>
)

expect(onError).toHaveBeenCalled()
const error = onError.mock.calls[0][0]
expect(error).toBeInstanceOf(Error)
expect(error.message).toBe('42')
})

it('should be reset by items of resetKeys, and call onReset', async () => {
const onReset = vi.fn()

Expand Down
17 changes: 13 additions & 4 deletions packages/react/src/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

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

getDerivedStateFromError is using any with an eslint disable. Since the method body immediately normalizes via convertToError, you can keep the React-typed signature (error: Error) or use unknown to avoid any and retain type-safety without suppressing the lint rule.

Copilot uses AI. Check for mistakes.

state = initialErrorBoundaryState
Expand All @@ -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
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

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

componentDidCatch is also typed as any with a lint suppression. Consider using the standard React signature (error: Error) or unknown, and then normalize with convertToError, so callers of onError remain strongly typed and you can drop the eslint disable.

Suggested change
// eslint-disable-next-line @typescript-eslint/no-explicit-any
componentDidCatch(error: any, info: ErrorInfo) {
componentDidCatch(error: unknown, info: ErrorInfo) {

Copilot uses AI. Check for mistakes.
this.props.onError?.(convertToError(error) as InferError<TShouldCatch>, info)
}

reset = () => {
Expand Down
Loading