[SalesForce] Unable to display validation message according to the need on custom lightning component

I have a custom component to create quote and also have few validation rules been created.
Whenever i am trying to create the quote, my validation rules are getting triggered and able to display them on UI, but
Expected: i should see only validation message
Actual: Im able to see the whole message like this.
ValidationError

I am expecting to display only the highlighted error to display on components UI.

Apex class:

 try{
                insert qu;          
            }catch(Exception e){            
                throw new AuraHandledException('Error Details : '+e.getMessage());           
            }

javascript

if(state == "ERROR"){
                        var errors = a.getError(); 
                        //console.log(errors);
                            component.set("v.showErrors",true);
                            component.set("v.errorMessage",errors[0].message);                           
                    }

Component

<aura:if isTrue="{!v.showErrors}">       
        <div class="slds-notify slds-notify_toast slds-theme_error">
            <span class="slds-assistive-text">error</span>
            <div class="slds-notify__content">
                <h5 class="slds-text-heading_small slds-align_absolute-center">Error Message </h5>
                <br/>
                <p class="slds-align_absolute-center">{!v.errorMessage}</p>                
            </div>
        </div>
    </aura:if>

Please help me with the solution

Best Answer

If you catch this exception in DmlException catch block then you will get only validation error message. This exception is caused by a failure in DML operation so it should be catch in DmlException catch block.

Catch DmlException in your code like this.

try{
       insert qu;
    }catch (DmlException ex) {
        throw new AurahandledException('Error Details : ' + ex.getDmlMessage(0));
    } catch (Exception ex) {
        throw new AurahandledException('Error Details : ' + ex.getMessage());
    }
Related Topic