Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google_Service_Exception error 500 not caught

I am using the google api client to get a list of blogger posts (see below code).

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();
    }

This works fine on most occasions, but occasionally I get an API error which is not caught.

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)"} []

Does anyone know why this error is thrown, and how I can deal with it?

Cheers

like image 606
Patrick Avatar asked Dec 25 '22 08:12

Patrick


1 Answers

The problem is with the namespaces. You can use the generic:

} catch (\Exception $e) {
    //print 'There was a general error : ' . $e->getMessage();
}

or, more specific:

try {
  ...
} catch (\Google_Service_Exception $e) {
  //print 'There was a general error : ' . $e->getMessage();
}

Exception is the base class for all Exceptions. See http://php.net/manual/en/class.exception.php

like image 172
Petrica Avatar answered Jan 07 '23 01:01

Petrica