Google-api-php-client: Explicit exceptions

Created on 23 Feb 2016  路  4Comments  路  Source: googleapis/google-api-php-client

Hi there.

I'm trying to catch exceptions from GoogleSDK, and this is not very easy to make something clean.

Some propositions

  1. Improve Google_Service_Exception interface with hasReason, getErrorsFromDomain ..
  2. Translate GoogleAPI Error to dedicated PHP Exception, like GooglePushWebHookUrlUnauthorized.

Regards.

Most helpful comment

Hi @bshaffer
You right, getErrors is available. But it simply return an array of un-typed array.

Right now,
I have a dedicated GoogleErrorsDecoder that is constructed with the result of Google_Service_Exception::getErrors.
An helper function hasError allow me to easily check the kind of error that is throw from GoogleAPI.

What I'm trying to say.
I can't use the try/catch with the exception matching system.

try {
  $this->subscribeToPushNotification($memberId);
}
catch (\Google_Service_Exception $e) {
  $errors = $e->getErrors();

  $webHookUrlUnauthorized = false;
  foreach($errors as $error) {
    if(isset($error["reason"]) && $error["reason"] === "push.webhookUrlUnauthorized") {
      $webHookUrlUnauthorized = true;
    }
  }

  if(!$webHookUrlUnauthorized) {
    throw $e;
  }
}

The above example is very complex to read..

try {
  $this->subscribeToPushNotification($memberId);
}
catch (\Google_WebHook_Url_Unauthorized $e) {
}

The above is very simple to read..

Regards.

All 4 comments

Please provide more information in the problem you're having and what you're trying to accomplish.

We have $exception->getErrors() which can be used for this purpose.

Hi @bshaffer
You right, getErrors is available. But it simply return an array of un-typed array.

Right now,
I have a dedicated GoogleErrorsDecoder that is constructed with the result of Google_Service_Exception::getErrors.
An helper function hasError allow me to easily check the kind of error that is throw from GoogleAPI.

What I'm trying to say.
I can't use the try/catch with the exception matching system.

try {
  $this->subscribeToPushNotification($memberId);
}
catch (\Google_Service_Exception $e) {
  $errors = $e->getErrors();

  $webHookUrlUnauthorized = false;
  foreach($errors as $error) {
    if(isset($error["reason"]) && $error["reason"] === "push.webhookUrlUnauthorized") {
      $webHookUrlUnauthorized = true;
    }
  }

  if(!$webHookUrlUnauthorized) {
    throw $e;
  }
}

The above example is very complex to read..

try {
  $this->subscribeToPushNotification($memberId);
}
catch (\Google_WebHook_Url_Unauthorized $e) {
}

The above is very simple to read..

Regards.

Creating an exception class for every possible error is not really something we want to implement. However, having a method for getReason that returns the first error's reason might be a convenient way to handle this.

Thank you for your feature request. This client library is currently in maintenance mode. We are fixing necessary bugs and adding essential features to ensure this library continues to meet your needs for accessing Google APIs. After careful consideration, we have decided not to proceed with this enhancement. If you disagree, please feel free to re-open this issue and explain why you believe it is essential for users of this client library.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

usamamashkoor picture usamamashkoor  路  5Comments

camohub picture camohub  路  3Comments

mandavister picture mandavister  路  4Comments

whatido1 picture whatido1  路  3Comments

slaFFik picture slaFFik  路  5Comments