Error Boundaries and Graceful Degradation

An error boundary (a class component with getDerivedStateFromError and componentDidCatch) catches render errors in its child components and shows a fallback…

A short circuit in the kitchen, and the whole house goes dark, because there's only one circuit breaker for every room. With a breaker per room, only the kitchen goes off, and the other rooms stay lit.

An app can have a "breaker" per part too. That's called graceful degradation: when one part of an app breaks, the other parts keep working, and the broken part shows a polite stand-in. In React, the tool for this is the error boundary: a component that catches errors thrown by the components inside it while they render, and shows a fallback (a replacement view) instead of turning the whole page blank.

Error boundary: a catcher for render errors

class ErrorBoundary extends React.Component {
  state = { hasError: false }

  static getDerivedStateFromError(error) {
    return { hasError: true } // the next render shows the fallback
  }

  componentDidCatch(error, info) {
    logError(error, info) // send it to monitoring, Sentry for example
  }

  render() {
    if (this.state.hasError) return this.props.fallback
    return this.props.children
  }
}

<ErrorBoundary fallback={<p>[Chart unavailable]</p>}>
  <Chart />
</ErrorBoundary>

One at the root or one per widget

// One boundary at the root
<ErrorBoundary fallback={<p>Something went wrong</p>}>
  <Header /> <Chart /> <Footer />
</ErrorBoundary>

// One boundary per widget
<ErrorBoundary fallback={<p>[Header unavailable]</p>}><Header /></ErrorBoundary>
<ErrorBoundary fallback={<p>[Chart unavailable]</p>}><Chart /></ErrorBoundary>
<ErrorBoundary fallback={<p>[Footer unavailable]</p>}><Footer /></ErrorBoundary>
Placement Chart throws What the user sees
One at the root yes only "Something went wrong", Header and Footer are gone too
Per widget yes "Welcome", "[Chart unavailable]", "© 2026"

With a boundary only at the root, one broken widget wipes out everything: Header and Footer disappear too, though they did nothing wrong. A boundary per widget keeps the "blast" small. A boundary at the root is still useful as the last safety net, for errors that slip past every other boundary.

What an error boundary doesn't catch

An error boundary only catches errors that happen while React is rendering the components inside it. What slips through:

Since React 19 there are two exceptions that still reach the nearest boundary: an error in the function passed to startTransition, even when it is called from onClick, and a rejected promise read with use().

For event handlers and async code, catch them yourself with try/catch, keep the message in state, then show it:

async function handleSave() {
  try {
    await saveDraft()
  } catch (e) {
    setError(e.message) // the boundary will never know, so we show it ourselves
  }
}

Graceful degradation without React: safelyRender

The idea is the same in plain code: put a guard per part, not one for everything. This is what you write in the exercise:

function safelyRender(widgets, log) {
  return widgets.map((widget) => {
    try {
      return widget.render()
    } catch (e) {
      log({ widget: widget.name, error: e.message })
      return "[" + widget.name + " unavailable]"
    }
  })
}
Widget render() Result What log records
Header "Welcome" Welcome
Chart throws Error("API down") [Chart unavailable] { widget: "Chart", error: "API down" }
Footer "© 2026" © 2026

Data from many APIs: Promise.all vs Promise.allSettled

A page that calls three APIs at once (user, posts, notifications) has the same problem. Promise.all rejects as soon as one promise fails, so one dead API makes the whole page fail. Promise.allSettled waits for all of them to finish and never rejects. Its result is an array of { status: "fulfilled", value } or { status: "rejected", reason }, in the same order as the promises.

async function allSettledFallback(promises, fallbacks) {
  const results = await Promise.allSettled(promises)
  return results.map((r, i) => (r.status === "fulfilled" ? r.value : fallbacks[i]))
}

allSettledFallback(
  [Promise.resolve("user"), Promise.reject("err"), Promise.resolve("notif")],
  [null, [], null],
).then((result) => {
  console.log(result[0])                 // user
  console.log(JSON.stringify(result[1])) // []
  console.log(result[2])                 // notif
})

🎯 Principles teams use

⚠️ Why one error can still wipe out everything

TL;DR: An error boundary (a class component with getDerivedStateFromError and componentDidCatch) catches render errors in its child components and shows a fallback. Put one per widget or section, plus one at the root as the last safety net, so a broken Chart only replaces itself with [Chart unavailable]. A boundary doesn't catch errors in event handlers, async code, SSR or itself (except, since React 19, errors in startTransition and a promise read with use()): catch those with try/catch and keep them in state. In plain code, safelyRender puts a try/catch inside map, logs { widget: name, error: e.message }, and returns "[Name unavailable]". For many APIs, Promise.allSettled waits for all of them and never rejects, and a failed one is replaced by the fallback in the same position, so the result is user, [], notif.

⚡ One breaker per part

Meaning: Graceful degradation: when one part of an app breaks, the other parts keep working, and the broken part shows a polite stand-in. In React, the tool is the error boundary: a component that catches render errors in its child components, then shows a fallback.

⚡ One per widget, not just at the root

Write:

<ErrorBoundary fallback={<p>[Chart unavailable]</p>}><Chart /></ErrorBoundary>
Guess first: there's only one boundary, at the root, and Chart throws. What happens to Header and Footer?

They disappear too. The user only sees "Something went wrong". With a boundary per widget, only Chart is replaced by [Chart unavailable].

Meaning: A boundary at the root is still useful as the last safety net.

⚡ What a boundary doesn't catch

Guess first: does an error boundary catch an error in onClick?

No. A boundary only catches errors while React is rendering. Errors in event handlers, async code (setTimeout, promises, fetch), SSR and the boundary itself slip through. The exceptions since React 19: an error inside startTransition and a promise read with use().

Meaning: For those, catch them yourself with try/catch, keep the message in state, then show it.

⚡ Many APIs: Promise.allSettled

Write:

const results = await Promise.allSettled(promises)
return results.map((r, i) => (r.status === "fulfilled" ? r.value : fallbacks[i]))

Meaning: Promise.all rejects as soon as one fails. Promise.allSettled waits for all of them and never rejects, so a failed one can be replaced by the fallback in the same position.

What you will learn