0

Using standard API, when i receive a POST, if things go wrong i must return an error response.

Since i'm not using a custom web service but a TriggerHandler, when one of the required parameters isn't in JSON POST, i'm using a custom exception to return a proper response. The problem is that the message shows the full stack trace.

Any way to deal with that?

CODE

private static void validateParams(List<CSVEnergyLead__c> lstToValidate){

    String strErrorMsg = null;

    for(CSVEnergyLead__c toValidate: lstToValidate){

        if(String.isBlank(toValidate.Stage__c)){
            strErrorMsg = 'Mandatory field Stage__c not found';
        }else if(String.isBlank(String.valueOf(toValidate.ClosingDate__c))){
            strErrorMsg = 'Mandatory field ClosingDate__c not found';
        }else if(String.isBlank(toValidate.Lead__c)){
            strErrorMsg = 'Mandatory field Lead__c not found';
        }

        if(!String.isBlank(strErrorMsg)){
            System.debug(LoggingLevel.ERROR, 'Throwing excepction: '+strErrorMsg);
            throw new CSVEnergyLeadException(strErrorMsg);
        }
    }

}

RESPONSE

[{
    "message": "CSVEnergyLeadTrigger: execution of BeforeInsert\n\ncaused by: CSVEnergyLeadTriggerHandler.CSVEnergyLeadException: Mandatory field Stage__c not found\n\nClass.CSVEnergyLeadTriggerHandler: line 113, column 1\nClass.CSVEnergyLeadTriggerHandler.bulkBefore: line 32, column 1\nClass.TriggerFactory.execute: line 38, column 1\nClass.TriggerFactory.createHandler: line 26, column 1\nTrigger.CSVEnergyLeadTrigger: line 2, column 1",
    "errorCode": "CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY",
    "fields": []
}]
  • 1
    You can't modify the response...but on the caller side, surely your calling app isn't just dumping the stack trace for end users to see, is it? Take a look at the X-Y problem and perhaps edit the question. – identigral Nov 18 '19 at 16:19
  • 1
    What standard API call is your service using? – Bryan Anderson Nov 18 '19 at 16:25
  • Excelent advise identigral I'll keep in mind the X-Y problem. Rest API Bryan – Héctor Cervera Panella Nov 18 '19 at 21:18
1

Use .addError on the record instead. This will show the relevant error without showing the full stack trace.

| improve this answer | |

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.