我正在使用google api客户端获取博客帖子列表(见下面的代码)。
try {
// create service and get data
$blogger = new Google_Service_Blogger($this->client);
// https://developers.google.com/blogger/docs/3.0/reference/posts/list
// GET https://www.googleapis.com/blogger/v3/blogs/blogId/posts
if ($this->cache->contains('posts')) {
$posts = $this->cache->fetch('posts');
}
else {
$posts = $blogger->posts->listPosts($this->bloggerId, $optParams);
$this->cache->save('posts', $posts, 600); // 600 = 10 mins
}
return $posts;
} catch (apiServiceException $e) {
// Error from the API.
//print 'There was an API error : ' . $e->getCode() . ' : ' . $e->getMessage();
} catch (Exception $e) {
//print 'There was a general error : ' . $e->getMessage();
}
这在大多数情况下工作得很好,但偶尔我会得到一个未捕获的API错误。
Google_Service_Exception: Error calling GET https://www.googleapis.com/blogger/v3/blogs/8894809254783984374/posts?maxResults=5: (500) Backend Error at /var/www/releases/20140607051057/vendor/google/apiclient/src/Google/Http/REST.php:79)"} []
有谁知道为什么会抛出这个错误,我该如何处理它?
欢呼
问题出在名称空间上。你可以使用泛型:
} catch (Exception $e) {
//print 'There was a general error : ' . $e->getMessage();
}
或者更具体的:
try {
...
} catch (Google_Service_Exception $e) {
//print 'There was a general error : ' . $e->getMessage();
}
Exception是所有Exception的基类。见http://php.net/manual/en/class.exception.php