# How do error boundaries and errorCallback methods work for handling component exceptions in LWC?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Error Handling, errorCallback

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-error-boundaries-and-errorcallback-methods-work-for-handling-component-exceptions-in-lwc

## Answer

**Basics:** Without a boundary, an exception in one child can blank out a large part of the page. A boundary component wraps risky children, catches the error, and swaps in a friendly message.

**How it works:**
1. Define `errorCallback(error, stack)` in a wrapper (ancestor) component.
2. When any descendant throws in a covered phase, the framework calls it with the error and a string describing the component stack.
3. Set a state field such as `this.error = error` to switch the template to fallback UI, and log details.
4. The wrapper template shows `<slot>` normally, or a fallback when `error` is set.

**Advanced / deep dive:** The boundary does not catch errors thrown in its own code, nor errors in asynchronous operations such as promises, `setTimeout`, or `async` handlers; those need `try/catch` or `.catch`. Errors in Apex calls surface as rejected promises, so handle them where made. Typical design: one generic `errorBoundary` component that wraps a page region, logs through an Apex logger or platform event (for example to a custom log object or a framework such as Nebula Logger), and offers a retry button that clears the error to re-render the children. Because rendering removes the failed subtree, retry creates fresh instances. Boundaries should be placed around independent regions so one failure does not hide others. In production the error message from some failures is generic, so include context in the log. For Aura, use `aura:handler event="aura:systemError"` or try/catch in controllers.

**Code / config example:**
```javascript
import { LightningElement } from 'lwc';
export default class ErrorBoundary extends LightningElement {
    error;
    errorCallback(error, stack) {
        this.error = error;
        console.error('Boundary caught:', error?.message, stack);
        // optionally call an Apex logger here
    }
    handleRetry() { this.error = undefined; }
}
```
```html
<template>
    <template lwc:if={error}>
        <p class="slds-text-color_error">Something went wrong.</p>
        <lightning-button label="Retry" onclick={handleRetry}></lightning-button>
    </template>
    <template lwc:else><slot></slot></template>
</template>
```

**Hidden gotchas & must-remember points:**
- It catches errors only from descendants, never from the component that defines it.
- Asynchronous failures (promises, timers) bypass `errorCallback`; use `try/catch`.
- You must render fallback UI yourself; the framework does not do it for you.
- Slotted children are descendants only if the boundary renders them, so keep the slot inside the boundary.
- Logging inside `errorCallback` must not itself throw.
- Do not swallow errors silently; log them.

**Best practice:** Wrap independent page regions in a reusable boundary component that logs the error and offers a retry.

---
Source: ForceChallenges · last updated 2026-11-02
