4

I have made a test Lightning app to figure out why error catching isn't working as I expected.

Component:

<aura:handler name="init" value="{!this}" action="{!c.doInit}" />

</aura:component>

Controller:

({
    // Handle component initialization
    doInit : function(component, event, helper) {
        $A.logger.subscribe("INFO", logCustom);
        $A.log('initialized logger');

        function logCustom(level, message, error) {
            console.log(getTimestamp());
            console.log(error);
            console.log(error.stack)
        }

        function getTimestamp() {
            return new Date().toJSON();
        }

    }

})

Error:

"Something has gone wrong. Error during init [Action failed: c$JQTest$controller$doInit [TypeError: Cannot read property 'subscribe' of undefined]]"

heroku lightning:lint

  error       aura-api     Invalid Aura API                    
  Line:4:9
          $A.logger.subscribe("INFO", logCustom);
           ^

Perhaps I'm super confused... is $A.logger no longer supported? If not what is the new preferred way to catch errors? Thanks!

4

With locker being activated you will not be able to use the undocumented API's and this was one of the reason why locker is in place

See the JavaScript API to find available methods and objects at https://myDomain.lightning.force.com/auradocs/reference.app, where myDomain is the name of your custom Salesforce domain.

The correct way to log the errors has been documented in release guide

<!--c:recoverableError-->
<aura:component>
<p>Click the button to trigger the controller to throw an error.</p>
<div aura:id="div1"></div>

<ui:button label="Throw an Error" press="{!c.throwErrorForKicks}"/>

The JS controller side function is as below

/*recoverableErrorController.js*/
 ({
    throwErrorForKicks: function(cmp) {
    // this sample always throws an error to demo try/catch
    var hasPerm = false;
    try {
        if (!hasPerm) {
            throw new Error("You don't have permission to edit this record.");
        }
    }
    catch (e) {
        $A.createComponents([
            ["ui:message",{
                "title" : "Sample Thrown Error",
                "severity" : "error",
            }],
            ["ui:outputText",{
                "value" : e.message
            }]
            ],
            function(components, status){
                if (status === "SUCCESS") {
                    var message = components[0];
                    var outputText = components[1];
                    // set the body of the ui:message to be the ui:outputText
                    message.set("v.body", outputText);
                    var div1 = cmp.find("div1");
                    // Replace div body with the dynamic component
                    div1.set("v.body", message);
                }
            }
        );
    }
  }
})
  • 1
    Thanks Mohith. I didn't see this explicitly referenced in the developers guide. What I like about the Logger approach is that I can have global error handling, this approach feels very clunky as I'll need to write TONS of try/catch boilerplate throughout every method. The problem I have is that there is clearly global-level error handling since I'll get the gray aura error box when things like ltng:require import a bad library, but I don't get direct link to the offending code in console and need to manually look up the line based on the error message :) – Rowan Christmas Jul 4 '16 at 15:43
  • Please let me know if you need anything else on this and if all good please mark it answered to help the community – Mohith Shrivastava Jul 4 '16 at 15:45
  • It certainly solves the problem, I just wish there was a better solution :) (see my edited comment above) – Rowan Christmas Jul 4 '16 at 15:48
  • 1
    @RowanChristmas I agree with you .I wish there was better way to do this and may be i am not aware so will wait if someone from sfdc lightning team writes something different here – Mohith Shrivastava Jul 4 '16 at 16:01

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.