Skip to main content

Do you know you can show error message in your lightning component with AuraException #LightningStrikes

 

Lightning Component

=====================================================================

<aura:component controller="clickMeCOntroller">

    <button onclick="{!c.clickMe}">Click Me</button>

</aura:component>

=====================================================================

the JS file

 

The controller.js

({

    clickMe : function(component, event, helper) {

        var action = component.get("c.clickMeServer");

        action.setCallback(this, function(response) {

            var state = response.getState();

            if (state === "SUCCESS") {

            }

            else if (state === "INCOMPLETE") {

                // do something

            }

            else if (state === "ERROR") {

                var errors = response.getError();

                if (errors) {

                    if (errors[0] && errors[0].message) {

                        showToast(errors[0].message,"error");

                    }

                } 

                else {

                    console.log("Unknown error");

                }

            }

        });

        $A.enqueueAction(action);

    }

})

=====================================================================

helper.js 

({

    showToast : function(message,status) {

            var toastEvent = $A.get("e.force:showToast");

            toastEvent.setParams({

                "title"   : "Status",

                 "mode"   : "sticky",

                 "type"   : status, 

                "message" : message

            });

            toastEvent.fire();

        }

})

=====================================================================

public class clickMeCOntroller{

     @AuraEnabled

     public static void clickMeServer(){

         try{

         }

         catch(exception e){

            throw new AuraHandledException('Exception' + e);           

         }

     }

}

0/9000