-
Notifications
You must be signed in to change notification settings - Fork 266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
grpc
extension changes exceptions being thrown.
#224
Comments
Good feedback. In case of an error, REST and gRPC transports do not return the metadata in the exact same format:
The current implementation of the This is not a bug per se, flagging this issue as an enhancement request. You are saying that there is more information about the error in the |
Yes, to get details about the error I do this: /** @var \Google\Ads\GoogleAds\Lib\V2\GoogleAdsException $e */
$errorArray = json_decode($e->getGoogleAdsFailure()->serializeToJsonString(), true);
# ApiException won't let me do that That gives me roughly this data which I can work with:
Why do I serialize errors to JSON and deserialize it back? Because this is the only way I figured out to get the actual error code and details about the error. Is there a better way? |
Thanks for sharing, To be sure, do you mind executing a |
Nice! Can you show an example I can use, say, for getting |
Sure, you could print the error codes using something like this: try {
// Does some API call(s)
} catch (ApiException $apiException) {
foreach($apiException->getMetadata() as $metadata) {
if(
array_key_exists('@type', $metadata) &&
$metadata['@type'] == 'type.googleapis.com/google.ads.googleads.v2.errors.GoogleAdsFailure' &&
array_key_exists('errors', $metadata)
) {
foreach($metadata['errors'] as $error) {
if(array_key_exists('errorCode', $error)) {
foreach($error['errorCode'] as $errorCode) {
print("Error Code: " . $errorCode);
}
}
}
}
}
} HIH |
Thank you! Let me play with that example. |
Tried this test, but it gave me no error codes. $budget = new CampaignBudget([
'name' => new StringValue([
'value' => sprintf('Budget for campaign [%s]', uniqid())
]),
'delivery_method' => BudgetDeliveryMethod::STANDARD,
'amount_micros' => new Int64Value(['value' => -100]) // wrong value cuases failure
]);
$op = new CampaignBudgetOperation();
$op->setCreate($budget);
$client = $this->app->make(GoogleAdsClientBuilder::class)->build($userId, $googleClientId);
$campaignBudgetServiceClient = $client->getCampaignBudgetServiceClient();
try {
$campaignBudgetServiceClient->mutateCampaignBudgets($googleClientId, [$op]);
} catch (ApiException $e) {
foreach($e->getMetadata() as $metadata) {
if(
array_key_exists('@type', $metadata) &&
$metadata['@type'] == 'type.googleapis.com/google.ads.googleads.v2.errors.GoogleAdsFailure' &&
array_key_exists('errors', $metadata)
) {
foreach($metadata['errors'] as $error) {
if(array_key_exists('errorCode', $error)) {
foreach($error['errorCode'] as $errorCode) {
print("Error Code: " . $errorCode);
}
}
}
}
}
} The API call logged:
PHP extensions:
|
The main condition I drafted might be overkill, did you try to simplify keeping only: array_key_exists('errors', $metadata) |
I was testing campaign creation and particular case: name duplication error.
I found out that different exceptions are thrown in the runtime:
grpc
is enabled I get\Google\Ads\GoogleAds\Lib\V2\GoogleAdsException
grpc
is disabled I get\Google\ApiCore\ApiException
Not to say, that first case is much more preferable, as the exception contains useful extra information about the problem.
The text was updated successfully, but these errors were encountered: