2

I am attempting to display an error message on a VF page if I get a time out error from an api call. I am using try catch, to get to the exception, but I would like to stop further code execution after.

Controller:

    try{
        gmsSearchResults = GMSServiceAPI.doAdvancedSearch(cfg); //perform the call

     }catch (CalloutException e){

         if(e.getMessage() == 'Read timed out'){

                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity.ERROR, ' We have a timeout Error. GMS did not return results for the allotted time.');
                ApexPages.addMessage(myMsg);
         }
     }

if(gmsSearchResults.size() > 49){
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity.INFO, ' Search returned more than 50 records. Please refine your search criteria.');
            ApexPages.addMessage(myMsg); 
        }

I am seeing the exception and the error in my debug log, but the rest of my code fails when it hits the if statement outside of the try/catch, with an error of "Attempt to de-reference a null object"

1 Answer 1

4

Simply add a return statement.

catch (CalloutException e)
{
    // handle error
    return;
}

If your method return type is PageReference rather than void, then change return to return null.

0

You must log in to answer this question.

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