My application has javascript button that call Apex code. Exceptions from web services are sometimes thrown and the user is shown a detailed exception message like this:
A problem with the OnClick JavaScript for this button or link was encountered:
{faultcode:'soapenv:Client', faultstring:'HttpException: Example Error Message
Class.BananaWebService.fetchBanana: line 22, column 1', }
Is there a simple way to create friendly error messages for my users when exceptions are thrown like this:
Example Error Message
Exception Class
public class HttpException extends Exception{}
Web Service Class
@RestResource(urlMapping='/BananaWebService/*')
global class BananaWebService {
webservice static void fetchBanana(Id accountId) {
throw new HttpException('Example Error Message');
}
}
Javascript code
{!REQUIRESCRIPT("/soap/ajax/28.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/28.0/apex.js")}
var accountId = "{!Account.Id}";
sforce.apex.execute("BananaWebService","fetchBanana", { accountId: accountId });
window.location.reload();