Rishi Pal
Rishi Pal

Reputation: 1

How to ignore error if deleteContact function in peopl api unable to get contact to delete

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 Google\Service\Exception:
{ "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): Google\Http\REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google\\Service\\...') 
#1 [internal function]: Google\Http\REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), '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): Google\Task\Runner->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);
}

Upvotes: 0

Views: 43

Answers (2)

abduhalimdev
abduhalimdev

Reputation: 1

You can handle the error gracefully and continue processing the remaining contacts by using a try-catch block to catch the Google\Service\Exception 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 (Google\Service\Exception $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.

Upvotes: -1

Shibon
Shibon

Reputation: 1574

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(Google\Service\Exception $ex) {
   //write this already deleted
}

Upvotes: 0

Related Questions