3

I found the documentation of errorCallback here - https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.reference_lifecycle_hooks.

But whatever I am trying to implement does not seem to work properly.

Implemented in parent component and throwing error in child component, But parent component is not able to handle or catch the error.

export default class Parent extends LightningElement {
    errorCallback(error, stack){
        console.log(error, stack);
    }
}

And in child if any error is happening, the standard error is being shown as modal and I am not getting anything in console log.

2 Answers 2

3

You can easily test this using a parent component and child component

Lets create a child component as below

child.html

<template>

</template>

child.js

 import { LightningElement } from 'lwc';

  export default class Child extends LightningElement {
    connectedCallback() {
       throw new Error('Whoops!');
  }
}

parent.html

<template>
   <c-child></c-child>
</template>

parent.js

 import { LightningElement } from 'lwc';

 export default class ParentCmp extends LightningElement {

    errorCallback(error, stack){
       throw error;
    }
 }

enter image description here

4
  • But I am getting the same error with/without errorCallback
    – Ramya
    Aug 10, 2019 at 3:21
  • @Ramya You wont . Try the example i have , if you comment the throw error in the Parent component you wont see any error Aug 10, 2019 at 3:31
  • So basically errorCallback is acting as collector for all the errors thrown by child components. Thanks!
    – Ramya
    Aug 10, 2019 at 3:35
  • Correct ! It will collect any error that the child component throws. Aug 10, 2019 at 4:10
0

As the document the above example states errorCallback() captures errors in all the child components in its tree during one of their lifecycle hooks. So you can use to catch those errors and may be show a generic message/toast

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .