Sub topic 10
errorCallback(): catching errors from child components
This chapter assumes you can write an LWC with an HTML template and a JavaScript class. It also assumes you know how connectedCallback and renderedCallback run in a parent and its children. Here you will learn how a parent component can catch errors thrown by its descendants and show a fallback view.
Why an error boundary helps
A child component can throw an error during its own lifecycle. For example, its constructor, connectedCallback(), render(), or renderedCallback() might fail. Without a handler, the error can break the whole component tree.
LWC lets an ancestor catch these errors with errorCallback(). A component that does this is often called an error boundary. It stops one failing child from taking down the whole page.
How errorCallback() works
You define errorCallback(error, stack) on the parent. LWC calls it when a descendant throws during a lifecycle hook. The method receives two arguments:
| Argument | Type | Meaning |
|---|---|---|
| `error` | JavaScript `Error` | The error thrown by the child. |
| `stack` | String | A string describing the component stack where the error happened. |
Three rules shape how you use it:
- It catches errors from any descendant, not only direct children.
- It does not catch errors thrown by the component that defines it. Those go to the next ancestor up.
- It catches errors raised in lifecycle hooks and rendering. It does not catch errors in event handlers or in asynchronous code such as promise callbacks, so handle those with
try...catchor.catch().
The diagram shows the path an error takes.
Building an error boundary
The simplest boundary stores the error in a field. Because fields are reactive, the template re-renders when the value changes.
import { LightningElement } from 'lwc';
export default class ErrorBoundary extends LightningElement {
error;
stack;
errorCallback(error, stack) {
this.error = error;
this.stack = stack;
}
}The template then chooses between the normal content and a fallback. It uses lwc:if and lwc:else for this. The child content goes in a <slot>, so any components placed inside the boundary are protected.
<template>
<template lwc:if={error}>
<p class="error">Something went wrong in this section.</p>
<p>{error.message}</p>
</template>
<template lwc:else>
<slot></slot>
</template>
</template>When a child throws, errorCallback() sets error. The component re-renders. The lwc:if branch replaces the slot, so the failing child is removed from the page and the message appears.
A parent can then use the boundary around any risky component:
<template>
<c-error-boundary>
<c-risky-widget></c-risky-widget>
</c-error-boundary>
</template>Choosing what to show
A fallback view should tell the user something useful without exposing internals. A good pattern is a short message for the user and the details for developers.
- Show a plain message such as "This section could not load."
- Log
errorandstackwithconsole.errorso developers can diagnose the problem. - Avoid printing the raw stack in the page in production.
If you want the user to retry, add a button that clears error. Setting this.error = undefined makes the template render the slot again, which creates the child again.
import { LightningElement } from 'lwc';
export default class RetryBoundary extends LightningElement {
error;
stack;
errorCallback(error, stack) {
console.error(error, stack);
this.error = error;
this.stack = stack;
}
handleRetry() {
this.error = undefined;
this.stack = undefined;
}
}A retry only helps when the failure was temporary. If the child fails the same way every time, it will throw again and the fallback will return.
Common mistakes
- Expecting the boundary to catch its own errors. If the component with
errorCallback()throws, the handler does not run for that error. Put the boundary one level above the code that might fail. - Expecting it to catch event handler errors. A click handler that throws is not routed to
errorCallback(). Usetry...catchinside the handler. - Rendering
errorbefore checking it exists. Thelwc:if={error}check keeps the template from readingerror.messageon an undefined value. - Swallowing errors silently. Storing the error without logging it hides the cause. Log it, or report it to your monitoring.
- Creating a loop. If the fallback template itself contains a component that throws, the boundary can keep failing. Keep the fallback simple, with plain markup.
Where it fits in the lifecycle
errorCallback() is the last hook in this tutorial's lifecycle model, and it differs from the others. The other hooks run as part of a component's own life: creation, insertion, rendering and removal. errorCallback() runs only when something goes wrong below the component, so many components never need it. Add it at meaningful boundaries, such as around a section of a page or a third-party widget, rather than on every component.
Exercise
A parent component defines errorCallback(). One of its child components throws an error inside a click event handler. Does errorCallback() run? What should you do instead?
Show answer
No. errorCallback() catches errors thrown during lifecycle hooks and rendering of descendants, not errors in event handlers. Wrap the handler body in try...catch and handle the error there, for example by setting a field that shows a message.
Exercise
You add errorCallback() to a component named Panel, and Panel's own connectedCallback() throws. Why does the fallback in Panel not appear, and where should the boundary go?
Show answer
A component's errorCallback() only receives errors from its descendants, not from itself. Move the boundary to a parent of Panel, and have that parent wrap Panel in its template.