Course contentsShow
← Web Development
Lesson 1669 of 2,98231. Error Handling and ResilienceFree lesson

Positioning Error Boundaries in the Component Tree

Strategic placement of error boundaries to isolate failures and determine granularity of error handling.

Positioning Error Boundaries in the Component Tree

You'll learn where to place error boundaries in your component hierarchy to balance error isolation with user experience.

The Strategic Placement Challenge

Think of error boundaries like firebreaks in a forest—you need to position them strategically to contain damage without creating too many divisions. Place them too high (near the root), and one small error crashes your entire app. Place them too low (around every component), and you lose meaningful context about what failed.

Placement Strategies

Top-Level Boundary: Always wrap your entire app with an error boundary as a safety net. This catches catastrophic failures and prevents the dreaded "white screen of death."

Feature-Level Boundaries: Wrap major features or routes separately. If your dashboard has a chart widget, sidebar, and data table, consider boundaries around each. A broken chart won't kill the sidebar.

Critical vs Non-Critical: Ask yourself: "If this fails, should the whole page break?" A comment section failing shouldn't crash your article reader. A payment form failing probably should prevent checkout completion.

Avoid Over-Granularity: Don't wrap every single component. Error boundaries add overhead, and too many create a jarring experience where pieces randomly disappear. Group related components under one boundary.

Practical Example Structure

<AppErrorBoundary>           ← Catch-all for entire app
  <Layout>
    <FeatureErrorBoundary>   ← Isolate major features
      <Dashboard />
    </FeatureErrorBoundary>
    <FeatureErrorBoundary>
      <UserProfile />
    </FeatureErrorBoundary>
  </Layout>
</AppErrorBoundary>

This gives you two layers: catastrophic protection at the top, and graceful feature degradation below.

Key Takeaway: Position error boundaries to isolate failures at the feature level while maintaining a root-level safety net—balance protection with user experience by thinking about blast radius and criticality.