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
Nicolas AstierNicolas Astier 

Apex error in lightning component

Hi,
I've hadded a lightning component's button to modify current opportunity via apex. 
I'm printing the result of the operation and in case of an error, I would like to print the error's message.
I'm inducing an error "update failed" but I can't seem to get the error message in the window
This is the part of my code to handle errors so far:
} else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        component.set("v.messageError", errors[0].message);
                    }
                    else {
                         component.set("v.messageError", errors.message);
                    } 
                } else {
                     component.set("v.messageError", "unknown error");
                }
}
<aura:iteration items="{!v.errorMessages2}" var="message2">
                        <li class="minli"><h3> ResultMessage: {!v.messageError} </h3></li>
</aura:iteration>
apex:
catch(Exception e) {
                throw e;
}

results:
WindowError in Logs


 
Best Answer chosen by Nicolas Astier
Tanuj TyagiTanuj Tyagi
Hi Nicolas ,
If you want to throw any error from apex and show it in lightning , Please try below code :
Put it inside catch block 
throw new AuraHandledException( ' Yor Error Message ==>'+e);

Hope it helps.

Cheers!!
Tanuj

All Answers

Tanuj TyagiTanuj Tyagi
Hi Nicolas ,
If you want to throw any error from apex and show it in lightning , Please try below code :
Put it inside catch block 
throw new AuraHandledException( ' Yor Error Message ==>'+e);

Hope it helps.

Cheers!!
Tanuj
This was selected as the best answer
Nicolas AstierNicolas Astier
Many thanks Tanuj, it works perfectly!