0

I'm using a calendar which syncs both automatically and under request (if designated by user) with G!Calendar. All works fine, except that I need to check if the calendar ID provided by the user exists.

For this I'm using:

$cal = "GOOGLE CALENDAR ID";
$service = new Google_Service_Calendar($client);
$title = $service->calendars->get($cal);

If the ID exists, the script runs fine. If it doesn't, then the page craches. How can I get the error JSON string it was supposed to return (as specified at the API resource page), so I can warn the user and block those errors from propagating?

Thanks in advance.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Lnunes
  • 15
  • 7

1 Answers1

0

You should do a try catch the error will be caught and you can display the message to the user.

function printCalendar($service, $fileId) {
  try {
    $calendar = $service->calendars->get($cal);
    print "Title: " . $file->getTitle();
  } catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
}
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449