Error Boundaries, Owner Stacks, and Root Error Handling
Production React applications need different mechanisms for different failure classes. A global window.onerror handler is not a complete error architecture.
Start by classifying the failureโ
Error Boundaries contain render failuresโ
class ErrorBoundary extends React.Component {
state = { error: null };
static getDerivedStateFromError(error) {
return { error };
}
componentDidCatch(error, info) {
reportError(error, { componentStack: info.componentStack });
}
render() {
return this.state.error ? this.props.fallback : this.props.children;
}
}
In React 19.2, the built-in Error Boundary lifecycle API is still class-based; maintained libraries can package the class for function-component applications.
What Error Boundaries do and do not ownโ
Do not use Error Boundaries as a universal JavaScript exception handler.
Boundary placement is product architectureโ
Boundary granularity should follow what users can recover from independently, not component-file boundaries.
Fallbacks are product behaviorโ
A useful fallback may provide:
- a scoped explanation;
- retry or safe navigation;
- an error/reference ID;
- preserved user work where possible;
- accessible focus/announcement behavior;
- telemetry correlation.
<ErrorBoundary
fallback={
<section role="alert">
<h2>We couldn't load this conversation.</h2>
<button onClick={retry}>Try again</button>
</section>
}
>
<Conversation />
</ErrorBoundary>
Reset semantics should follow domain identityโ
<ErrorBoundary key={conversationId} fallback={<ConversationCrash />}>
<Conversation id={conversationId} />
</ErrorBoundary>
Libraries may also provide explicit reset APIs. Choose reset behavior from the product's identity model.
Suspense and Error Boundaries solve different statesโ
<ErrorBoundary fallback={<AlbumsError />}>
<Suspense fallback={<AlbumsSkeleton />}>
<Albums />
</Suspense>
</ErrorBoundary>
Do not wrap use(promise) in ordinary try/catch to replace Suspense/Error Boundary behavior.
JavaScript stack vs Component Stack vs Owner Stackโ
componentDidCatch and root error callbacks can provide errorInfo.componentStack. Source maps should connect minified production JavaScript stacks to original code.
captureOwnerStack() is a development diagnosticโ
import * as React from 'react';
if (process.env.NODE_ENV !== 'production') {
const ownerStack = React.captureOwnerStack?.();
console.log(ownerStack);
}
Current React documentation says captureOwnerStack() returns string | null, is available only in development, and is useful during React-controlled execution such as render, Effects, React event handlers, and root error handlers. It can be unavailable in detached callbacks such as setTimeout or custom DOM event listeners.
Do not make production telemetry depend on Owner Stacks.
Root-level error callbacksโ
React client roots expose callbacks for caught, uncaught, and recoverable React-managed errors:
const root = createRoot(container, {
onCaughtError(error, errorInfo) {
report('caught', error, errorInfo);
},
onUncaughtError(error, errorInfo) {
report('uncaught', error, errorInfo);
},
onRecoverableError(error, errorInfo) {
report('recoverable', error, errorInfo);
},
});
Error architecture checklistโ
A strong error system tells users what can recover, tells engineers where the failure happened, and avoids confusing development-only diagnostics with production guarantees.