1

I am trying to implement aura:systemError by going through the docs link

but it is not working. showSystemError method in controller.js is not getting triggered.

Component:

    <aura:component controller="ExceptionClass" access="global" >
    <aura:handler event="aura:systemError" action="{!c.showSystemError}"/>
    <aura:attribute name="response" type="Aura.Action"/>
   <lightning:button name="clickMe" value="click me" label="click" onclick = "{!c.clickMe}"/>
   </aura:component>

Controller.js

 ({
    clickMe: function(cmp, event) {
    // Call an Apex controller that throws an error
    var action = cmp.get("c.throwError");
    action.setCallback(cmp, function(response){
        alert('hi'); // **getting called**
        cmp.set("v.response", response);
    });
    $A.enqueueAction(action);
},

showSystemError: function(cmp, event) {
    alert('hellllo'); // **not working**
    // Handle system error
    console.log(cmp);
    console.log(event);
}
})

ExceptionClass:

public class ExceptionClass {
@AuraEnabled
public static void throwError(){
    try{
        integer i =  1/0;
    }
    catch(Exception ex){
      throw ex;
    }
}
}

Does anyone have implemented it. Thanks in advance :)

0

I tried capturing aura:systemError but was unsuccessfull. It looks like aura:systemError was in earlier API but now deprecated.

What you are trying to achieve can be done using state variable in response.

trigger: function(cmp, event) {
        // Call an Apex controller that throws an error
        var action = cmp.get("c.throwError");
        action.setCallback(cmp, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                // Alert the user with the value returned 
                // from the server
                alert("From server: " + response.getReturnValue());
            }
            if (state === "ERROR") {
                console.log("From server: "+response.getReturnValue());
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                    errors[0].message);
                    }
                }
            }
        });
        $A.enqueueAction(action);
    },

Whenever server side error/exception happens the state variable from response is "ERROR". You can use that to your advantage to build the catching logic or firing events to alert other components.

  • 1
    Thanks Pranay. I thought aura:systemError is something new, so I was trying to see how it works. And yes, you are right, we can handle exception using state. – Arpit Sethi Apr 19 '18 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.