function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sudheer Kumar 134Sudheer Kumar 134 

If there is an exception in apex controller, should show that exception once click on the lwc button

Hi 
I am having an issue that, if i click on lwc button it is showing success toast message if there was an exception also, Iam able to see that error in logs  it is not catching the error. This always gives me an error on the console as "Uncaught (in promise)" and with the details of the exception. How can I catch this exception in a handled way.

here is my code

JS.

sendcomm({ recordId: this.recordId})
                try{
                    this.showToast('Success!', 'Communication sent successfully.', 'success', 'dismissable');
               
                }catch(error){
                    console.log('$#$#'+error.body.message);
                    this.showToast('Error!', 'error.body.message', 'error', 'dismissable');
                };
                this.handleIsLoading(false);
            }

Any help will be appreciable, thanks in advance

Hitesh chaudhariHitesh chaudhari
<!-- HTML--->

<template>
    <lightning-input type="text" label="Enter First Name"></lightning-input>
    <lightning-button variant="brand" label="Save" onclick={save} class="slds-m-left_x-small" ></lightning-button>

</template> 


JS code : 
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
@track firstName ;

save()
{
    if(this.firstName == null)
    {
        this.showToast('Warning !','Please enter first name','warning' );
    }
    else
    {
        // Call save function save method to perform apex commit
    }
}

showToast(titleVar,messageVar,variantVar) {
        const evt = new ShowToastEvent({
            title: titleVar,
            message: messageVar,
            variant: variantVar,
            mode: 'dismissable'
        });
        this.dispatchEvent(evt);
    }