Currently when Apex code on a VisualForce page throws an exception the user is shown what line of code threw the exception and some other information. I want to change this to show a custom error message, something along the lines of "Something went wrong, please contact Software Development at [email protected]."
1 Answer
Have you ever tried exception handling via Try-Catch block?
Put your code inside a try block and catch block with catch the exception if any exception occurs. You can add the custom exception message to apexPages variable
try{
//Your code goes here
}Catch(Exception e){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Your custom message'));
//You can also access exception string by e.getMessage() method
}
More details about exception class - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_exception_methods.htm
Modify your VF Page to include a tag and it will display the error when added by your apex class.
-
yep - totally agree -- and if I were in "Software Development" @ company.com, and this was an otherwise unexpected, albeit caught, exception, I'd like the ApexPages.Message to include the value of
e.getStackTraceString()
so I might be able to diagnose/fix the problem faster rather than having to force the user to reproduce the problem with debug logs on.– cropredyCommented Jan 14, 2016 at 1:02