skip to Main Content

We have a local system sync with google contact. I am facing issue in deleting multiple contacts in a loop. Lets 10 contacts had been deleted from local system. Then I stored the resourceName of deleted contact in the database and later when cron is executed these contacts are deleted from google contact. It is working fine for me if all the contacts to be deleted exist in Google Contact. But in case if any contact have been already deleted from google contact by our client then my cron does not work properly. When one of the 10 contacts does not exist in google contact then people api throw the errors and my php page halted.

I just want to ignore the error if contact to be deleted does not exist in google contact.

Below is my code

$a_deleteContact = array("people/1XXXXXXXXXXX","people/2XXXXXXXXXXX","people/3XXXXXXXXXXX",'"people/4XXXXXXXXXXX"');  

foreach($a_deleteContact as $contactResourceName)
{
    $client = new Google_Client();          
    $accessToken = json_decode(file_get_contents($this->tokenPath), true);
    $client->setAccessToken($accessToken);          
    $service = new Google_Service_PeopleService($client);       
                
    $profile = $service->people->get($contactResourceName,array('personFields' => 'metadata'));
    $etag = $profile->etag;
    if(!empty($etag))
    {
        $service->people->deleteContact($contactResourceName);
    }
}

In above example if resourceName people/1XXXXXXXXXXX does not exist on Google contact then foreach loop stopped and other existing contacts are not deleted.

I am getting following error from the api. I want to ignore this fatal error so that my script can delete other contacts.

Fatal error: Uncaught GoogleServiceException:
{ "error": { "code": 404, "message": "Requested entity was not found.", "errors": [ { "message": "Requested entity was not found.", "domain": "global", "reason": "notFound" } ], "status": "NOT_FOUND" } }
in /public_html/vendor/google/apiclient/src/Http/REST.php:128 
Stack trace: #0 /public_html/vendor/google/apiclient/src/Http/REST.php(103): GoogleHttpREST::decodeHttpResponse(Object(GuzzleHttpPsr7Response), Object(GuzzleHttpPsr7Request), 'Google\Service\...') 
#1 [internal function]: GoogleHttpREST::doExecute(Object(GuzzleHttpClient), Object(GuzzleHttpPsr7Request), 'Google\Service\...')
#2 /public_html/vendor/google/apiclient/src/Task/Runner.php(182): call_user_func_array(Array, Array) 
#3 /public_html/vendor/google/apiclient/src/Http/REST.php(66): GoogleTaskRunner->run() 
#4 /public_html/vendor/google/apiclient/src/Client.php(898): in /public_html/vendor/google/apiclient/src/Http/REST.php on line 128

Before deleting the contact from Google first I tried to check whether that record exist or not. But get() function throwing fatal error whereas I was expecting that it will return an empty $profile->etag.

$profile = $service->people->get($contactResourceName,array('personFields' => 'metadata'));
$etag = $profile->etag;
if(!empty($etag))
{  
    $service->people->deleteContact($contactResourceName);
}

2

Answers


  1. You can try by using try catch block

    try{
     $profile = $service->people->get($contactResourceName,array('personFields' => 'metadata'));
        $etag = $profile->etag;
        if(!empty($etag))
        {
            $service->people->deleteContact($contactResourceName);
        }
    } catch(GoogleServiceException $ex) {
       //write this already deleted
    }
    
    Login or Signup to reply.
  2. You can handle the error gracefully and continue processing the remaining contacts by using a try-catch block to catch the GoogleServiceException and ignore it. Here’s how you can modify your code:

     $a_deleteContact = array("people/1XXXXXXXXXXX", "people/2XXXXXXXXXXX", "people/3XXXXXXXXXXX", "people/4XXXXXXXXXXX");
    
    foreach ($a_deleteContact as $contactResourceName) {
        $client = new Google_Client();
        $accessToken = json_decode(file_get_contents($this->tokenPath), true);
        $client->setAccessToken($accessToken);
        $service = new Google_Service_PeopleService($client);
    
        try {
            $profile = $service->people->get($contactResourceName, array('personFields' => 'metadata'));
            $etag = $profile->etag;
            if (!empty($etag)) {
                $service->people->deleteContact($contactResourceName);
            }
        } catch (GoogleServiceException $e) {
            // Catch the exception and handle it gracefully (e.g., log it) without halting the script.
            error_log("Error deleting contact with resourceName: $contactResourceName - " . $e->getMessage());
        }
    }
    

    With this modification, if a contact does not exist in Google Contacts, it will catch the exception and log an error message, but it won’t stop the loop from continuing to process the remaining contacts.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search