ForceChallenges
ProblemsDailyNotesInterview
Sign in

Practice

All problemsSOQL practiceApex trigger challengesDaily challengeOpportunityTrigger.oldMapBefore UpdateBulkificationContactValidationGROUP BYSemi-JoinAccountBefore Insert

Interview questions

All questionsAdminAdministrationApexApex TriggerAsync ApexDevelopmentField ServiceField ServiceIntegrationLwc

By company

DeloitteSalesforceAccentureInfosysPwCCognizant

Learn

Study notes

ForceChallenges

AboutContact usPrivacy PolicyTerms of ServiceFor AI assistants (llms.txt)
© 2026 ForceChallenges · Salesforce SOQL & Apex trigger practice
LWC questions17 of 50
LWCMedium 2 min read

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 {  }  ;
      {
    error;
    () {
        . = error;
        .(, error?., stack);
        
    }
    () { . = ; }
}
Practice SOQL & Apex
PreviousHow does renderedCallback function, and what precautions should developers take to avoid infinite loops?Next What are the advantages of leveraging Salesforce Lightning Base Components over native HTML elements?

More LWC questions

  • How do you create custom modal dialog windows in LWC using LightningModal?
  • How can developers utilize lightning/uiListApi to fetch and display standard list view data?
  • How do you subscribe to Platform Events and streaming channels in LWC using lightning/empApi?
  • How can developers navigate to object home views and record creation pages using NavigationMixin?
  • How does the LWC reactivity engine manage property observation and virtual DOM updates?
LightningElement
from
'lwc'
export
default
class
ErrorBoundary
extends
LightningElement
errorCallback
error, stack
this
error
console
error
'Boundary caught:'
message
// 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.

LWCError HandlingerrorCallback