我正在使用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)"} []
有谁知道为什么会抛出这个错误,以及如何处理它?</ p>
干杯
答案 0 :(得分:4)
问题在于命名空间。您可以使用通用:
} 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();
}
异常是所有异常的基类。见http://php.net/manual/en/class.exception.php