user1344131
user1344131

Reputation: 225

google calendar error handling

I'm using V3 of the google calendar api to sync with a calendar in a php based application.

I use this code to delete events on the google calendar.

$delJson = json_decode($service->events->delete('primary', $gcal_id), true);

Everything runs smoothly if the google calendar id is found. However, the entire process STOPS and displays a blank page if there is no match found between $gcal_id and an actual google calendar id.

At the very least, I want the process to continue without coming to complete halt. I imagine the code would logically be something like this but I can't find any documentation on it.

$delJson = json_decode($service->events->delete('primary', $gcal_id), true);

if ($response == error) { continue } 

Upvotes: 0

Views: 1702

Answers (1)

Charlie
Charlie

Reputation: 138

I'm surprised this one has not been answered here, or much of anywhere else; I had the same problem and see it has been asked numerous times without much help answering it.

The V3 PHP api uses exceptions and the one you need to handle for this is probably the same as for an update(), using a Google_Service_Exception similar to below:

try {
    $cal->events->update($this->calName,$e,$event);
} catch (Google_Service_Exception $e ) {
    echo "Caught Google_Service_Exception:";
    print_r($e);
}

Note, if you have a get, update, you'd need to put the try around the get as well as the update. There are several other exception types in the API and presumably those as well can cause uglies for your website's visitors if not handled. Google_Auth_Exception, Google_IO_Exception, Google_Cache_Exception. So maybe best just to put a generic exception catcher around google API code unless you've studied the particular case to be sure you handle various possible errors.

Upvotes: 4

Related Questions