Skip to main content

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โ€‹

What kind of failure is this?
Descendant render/lifecycle failureError Boundary for UI containment
Caught/uncaught/recoverable root errorcreateRoot/hydrateRoot error callbacks for reporting
Need development ownership contextcaptureOwnerStack() when React context is available
Event/network/business failureExplicit JavaScript/result/error-state handling
SSR/process/server failureServer/framework/platform observability

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;
}
}
Boundary lookup follows the rendered tree
Descendant throws during React render work
render/lifecycle failure
Error Boundary
records failure + renders fallback
Healthy ancestor/sibling UI
can remain usable

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โ€‹

Good fit
descendant rendering ยท class lifecycle failures ยท render-path failures surfaced through React
Different mechanism
event handlers ยท ordinary detached async callbacks ยท server-render failures ยท failures outside the React tree

Do not use Error Boundaries as a universal JavaScript exception handler.

Boundary placement is product architectureโ€‹

Contain failures at useful recovery boundaries
App shell
navigation + global frame
Conversation list boundary
list can fail independently
Message thread boundary
active thread can fail independently
Optional widget boundary
non-critical integration

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>
A key can intentionally create a fresh failure boundary
Conversation A
boundary in failed state
Conversation B
new boundary + fresh subtree

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>
Pending and failed async work take different paths
Promise pending
nearest Suspense fallback
Promise rejected as an error
nearest Error Boundary fallback

Do not wrap use(promise) in ordinary try/catch to replace Suspense/Error Boundary behavior.

JavaScript stack vs Component Stack vs Owner Stackโ€‹

Three stacks answer different debugging questions
JavaScript stack
Where did code throw?
Component Stack
Where is the failing component rendered?
Owner Stack
Which components created the React nodes leading here?

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.

Capture ownership context before leaving React-controlled execution
React-controlled callback
Owner Stack available when supported
Later callback
Owner Stack may be null

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);
},
});
Containment and reporting are separate responsibilities
Error Boundary
user-facing containment/recovery
Root callbacks
central classification + observability
Telemetry
release ยท route ยท component stack ยท trace ID

Error architecture checklistโ€‹

1Classify failure
2Contain at product boundary
3Report centrally
4Preserve stack/context
5Offer recovery
6Correlate with release/trace

A strong error system tells users what can recover, tells engineers where the failure happened, and avoids confusing development-only diagnostics with production guarantees.