Google API错误处理无法收到错误消息

时间:2016-06-13 11:13:15

标签: php google-api google-calendar-api

谷歌日历API返回无效事件ID的错误我使用try catch进行错误处理,但错误响应来自如下对象,

Google_Service_Exception Object
(
    [errors:protected] => Array
        (
            [0] => Array
                (
                    [domain] => global
                    [reason] => deleted
                    [message] => Resource has been deleted
                )

        )

    [retryMap:Google_Service_Exception:private] => Array
        (
            [500] => -1
            [503] => -1
            [rateLimitExceeded] => -1
            [userRateLimitExceeded] => -1
        )

    [message:protected] => Error calling DELETE https://www.googleapis.com/calendar/v3/calendars/primary/events/qdkeiablias0t17vn3kh5aopq0_20160614T000000Z: (410) Resource has been deleted
    [string:Exception:private] => 
    [code:protected] => 410
    [file:protected] => C:\xampp\htdocs\sys-feedback\application\vendor\google\apiclient\src\Google\Http\REST.php
    [line:protected] => 110
    [trace:Exception:private] => Array
        (

如何使用php ...

获取 message

我也使用了JSON DECODE但它什么都没有返回

foreach ($event_ids as $eventID):
        try {
            $this->service->events->delete('primary', $eventID);
        } catch (Google_Service_Exception $e) {


        }


    endforeach;
    $result = json_decode($e,true);
    print_r($result;

当我echo $e['errors'][0]['message'];时,它会返回Cannot use object of type Google_Service_Exception as array in

1 个答案:

答案 0 :(得分:3)

对于错误的标题,您可以$e->getMessage()

try {
    $this->service->events->delete('primary', $eventID);
} catch (Google_Service_Exception $e) {
    $e->getMessage();           // This works
    print_r($e->getErrors());   // Not sure yet, have to test
}

如上所述,关于$e->getErrors(),我还不确定,仍然需要尝试。