0
@AuraEnabled
public static String getCurrentShares(final Id recordId) {
    if (!hasEdit(recordId)){
        throw new AuraHandledException('Only the record owner or administrators can access permissions');
    }

    List<SObject> results = Database.query(buildQuery(recordId));
    List<ShareWrapper> output = new List<ShareWrapper>();

    Set<Id> groupAndUserIDs = new Set<Id>();

    for (SObject so : results) {
        ShareWrapper sh = new ShareWrapper();
        sh.RowCause = String.valueOf(so.get('RowCause'));
        sh.AccessLevel = String.valueOf(so.get(getAccessLevelField(objectTypeFromId(recordId))));
        sh.UserOrGroupId = (Id)so.get('UserOrGroupId');
        groupAndUserIDs.add(sh.UserOrGroupId);
        output.add(sh);
    }

    Map<Id, User> userMap = new Map<Id, User>([SELECT Name FROM User WHERE Id IN :groupAndUserIDs]);
    Map<Id, Group> groupMap = new Map<Id, group>([SELECT Name FROM Group WHERE Id IN :groupAndUserIDs]);

    for (ShareWrapper sh : output) {
        if (userMap.get(sh.UserOrGroupId) != null) {
            sh.UserOrGroupType = 'User';
            sh.UserOrGroupName = userMap.get(sh.UserOrGroupId).Name;
        } else if (groupMap.get(sh.UserOrGroupID) != null){
            sh.UserOrGroupType = 'Group';
            sh.UserOrGroupName = groupMap.get(sh.UserOrGroupId).Name;
        }
    }


    return JSON.serialize(output);
}

//Test Class for SharingComponentController Test

@isTest

public class SharingComponentControllerTest{

 try{

          SharingComponentController.getCurrentShares(acc.id);
         }catch(AuraHandledException e) {
             System.debug(e.getMessage());

            System.assert(e.getMessage().contains('My Error Message'));

         }       


}
1
  • System.assertEquals('System.AuraHandledException: ' + message, String.valueOf(e));
    – Aequitas
    Jul 20, 2018 at 6:09

1 Answer 1

0

You cannot handle AuraHandledException in Apex itself.

My way to do is:

@isTest

public class SharingComponentControllerTest{

 try{
          SharingComponentController.getCurrentShares(acc.id);
          System.assert(false);
         }catch(Exception e) {
            System.assert(true);
             System.debug(e.getMessage());
            //System.assert(e.getMessage().contains('My Error Message'));
         }       
}

But I do not think you can get the error message. I agree it is not the best way to do so.

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