49

It seems like when I call getMessage on a thrown AuraHandledException, I see "Script-thrown Exception" instead of the message that I have set. Any idea why this may be happening?

Controller:

    @AuraEnabled
    public static Conversation__c makeConversation(String tonum, String objid)
    {
        if (tonum.length() != 10)
            throw new AuraHandledException('ToNumber length invalid.');
    }

TestController:

@isTest
    static void testMakeConversationInvalidTo()
    {
        Boolean testPass = false;
        String fromNumber = '1234567890';
        String toNumber = '1';

        List<ConversationController.ConversationWrapper> test =    ConversationController.getConversations();
        System.assertEquals(0, test.size());
        try {
            ConversationController.makeConversation(toNumber, '0');
        } catch (AuraHandledException e) {
            System.debug(e.getMessage());
            System.debug(e.getTypeName());
            System.assert(e.getMessage().contains('ToNumber length invalid'));
            testPass = true;
        }
    }

Return:

16:26:28:477 USER_DEBUG [320]|DEBUG|Script-thrown exception
16:26:28:477 USER_DEBUG [321]|DEBUG|System.AuraHandledException
4
  • One observation here is that your controller isn't set up to return anything back to the Aura Client Side of things. Try at least returning "this" or something similar. If you look a the docs there's a return set-up for the method. I don't see anything similar in your class to do that.
    – crmprogdev
    Commented May 22, 2016 at 20:46
  • @crmprogdev : the full controller does have return data, I left it out in this question as it is irrelevant.
    – Jason Lee
    Commented May 23, 2016 at 5:49
  • 1
    @JasonLee - Can you add debugs in your controller to check on which line is it throwing the exception.
    – Sneha
    Commented May 23, 2016 at 9:08
  • @JasonLee Other than telling you to add more debug statements to your code to confirm what is happening, I don't think anyone can help you unless you post more of your code. You've not provided sufficient code to reproduce the problem with your controller. If you want answer you'll find helpful, I'd urge you to post more of your code. If this didn't have a bounty on it, I'd be inclined to vote to close your question.
    – crmprogdev
    Commented May 23, 2016 at 11:09

1 Answer 1

103
+50

The problem is that the constructor for the AuraHandledException only sets the string argument to be the message sent to the client-end, while calling getMessage() on the exception through apex returns a different string that the constructor defaults to the value "Script-thrown Exception". Obviously, this leaves something to be desired for testing purposes, and I would suggest also adding the message to the exception with setMessage() so that it can be accessed through apex:

@AuraEnabled
public static void makeConversation(String tonum, String objid)
{
    if (tonum.length() != 10){
        AuraHandledException e = new AuraHandledException('ToNumber length invalid');
        e.setMessage('ToNumber length invalid');
        system.debug(e);
        throw e;
    }
}
5
  • Thank you, this definitely solves my issue, I did not know AuraHandledExceptions behaved like that..
    – Jason Lee
    Commented May 23, 2016 at 20:20
  • No problem. I agree that it is a bit counter-intuitive that there are different error messages for the client and server.
    – martin
    Commented May 24, 2016 at 12:11
  • Does anyone know the "why" behind this behavior? It seems like there must be a reason it is implemented like this but I cannot figure out why that would be.
    – dsharrison
    Commented Jul 19, 2017 at 16:40
  • My thinking is that the 'throw' exception when not saved to a variable is intended only to kick the error immediately back to the client. When we are testing there is no client so we are dealing in system messages only. Saving the exception to a variable gives the system message something to pass back to the test class rather than a generic system exception.
    – binaryLady
    Commented Dec 15, 2017 at 15:51
  • I encountered the exact same issue today and this thread resolved my puzzle!
    – Xi Xiao
    Commented Dec 27, 2018 at 19:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .