Error Handling Samples

The code samples below provide examples of common error handling functions using the AdWords API. Client Library.

Handle partial failures

// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package adwords.axis.v201708.errorhandling;

import com.beust.jcommander.Parameter;
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
import com.google.api.ads.adwords.axis.v201708.cm.AdGroupCriterion;
import com.google.api.ads.adwords.axis.v201708.cm.AdGroupCriterionOperation;
import com.google.api.ads.adwords.axis.v201708.cm.AdGroupCriterionReturnValue;
import com.google.api.ads.adwords.axis.v201708.cm.AdGroupCriterionServiceInterface;
import com.google.api.ads.adwords.axis.v201708.cm.ApiError;
import com.google.api.ads.adwords.axis.v201708.cm.BiddableAdGroupCriterion;
import com.google.api.ads.adwords.axis.v201708.cm.FieldPathElement;
import com.google.api.ads.adwords.axis.v201708.cm.Keyword;
import com.google.api.ads.adwords.axis.v201708.cm.KeywordMatchType;
import com.google.api.ads.adwords.axis.v201708.cm.Operator;
import com.google.api.ads.adwords.lib.client.AdWordsSession;
import com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface;
import com.google.api.ads.adwords.lib.utils.examples.ArgumentNames;
import com.google.api.ads.common.lib.auth.OfflineCredentials;
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
import com.google.api.ads.common.lib.utils.examples.CodeSampleParams;
import com.google.api.client.auth.oauth2.Credential;
import java.util.ArrayList;
import java.util.List;

/**
 * This example demonstrates how to handle partial failures.
 *
 * <p>Credentials and properties in {@code fromFile()} are pulled from the
 * "ads.properties" file. See README for more info.
 */
public class HandlePartialFailures {

  private static class HandlePartialFailuresParams extends CodeSampleParams {
    @Parameter(names = ArgumentNames.AD_GROUP_ID, required = true)
    private Long adGroupId;
  }

  public static void main(String[] args) throws Exception {
    // Generate a refreshable OAuth2 credential.
    Credential oAuth2Credential = new OfflineCredentials.Builder()
        .forApi(Api.ADWORDS)
        .fromFile()
        .build()
        .generateCredential();

    // Construct an AdWordsSession.
    AdWordsSession session = new AdWordsSession.Builder()
        .fromFile()
        .withOAuth2Credential(oAuth2Credential)
        .build();

    AdWordsServicesInterface adWordsServices = AdWordsServices.getInstance();

    HandlePartialFailuresParams params = new HandlePartialFailuresParams();
    if (!params.parseArguments(args)) {
      // Either pass the required parameters for this example on the command line, or insert them
      // into the code here. See the parameter class definition above for descriptions.
      params.adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
    }

    runExample(adWordsServices, session, params.adGroupId);
  }

  public static void runExample(
      AdWordsServicesInterface adWordsServices, AdWordsSession session, Long adGroupId)
      throws Exception {
    // Enable partial failure.
    session.setPartialFailure(true);

    // Get the AdGroupCriterionService.
    AdGroupCriterionServiceInterface adGroupCriterionService =
        adWordsServices.get(session, AdGroupCriterionServiceInterface.class);

    List<AdGroupCriterionOperation> operations = new ArrayList<AdGroupCriterionOperation>();

    // Create keywords.
    String[] keywords =
        new String[] {"mars cruise", "inv@lid cruise", "venus cruise", "b(a)d keyword cruise"};
    for (String keywordText : keywords) {
      // Create keyword
      Keyword keyword = new Keyword();
      keyword.setText(keywordText);
      keyword.setMatchType(KeywordMatchType.BROAD);

      // Create biddable ad group criterion.
      BiddableAdGroupCriterion keywordBiddableAdGroupCriterion = new BiddableAdGroupCriterion();
      keywordBiddableAdGroupCriterion.setAdGroupId(adGroupId);
      keywordBiddableAdGroupCriterion.setCriterion(keyword);

      // Create operation.
      AdGroupCriterionOperation keywordAdGroupCriterionOperation =
          new AdGroupCriterionOperation();
      keywordAdGroupCriterionOperation.setOperand(keywordBiddableAdGroupCriterion);
      keywordAdGroupCriterionOperation.setOperator(Operator.ADD);
      operations.add(keywordAdGroupCriterionOperation);
    }

    // Add ad group criteria.
    AdGroupCriterionReturnValue result =
        adGroupCriterionService.mutate(operations.toArray(new AdGroupCriterionOperation[] {}));

    // Display results.
    for (AdGroupCriterion adGroupCriterionResult : result.getValue()) {
      if (adGroupCriterionResult.getCriterion() != null) {
        System.out.printf("Ad group criterion with ad group ID %d, and criterion ID %d, "
            + "and keyword '%s' was added.%n", adGroupCriterionResult.getAdGroupId(),
            adGroupCriterionResult.getCriterion().getId(),
            ((Keyword) adGroupCriterionResult.getCriterion()).getText());
      }
    }

    for (ApiError apiError : result.getPartialFailureErrors()) {
      // Get the index of the failed operation from the error's field path elements.
      FieldPathElement[] fieldPathElements = apiError.getFieldPathElements();
      FieldPathElement firstFieldPathElement = null;
      if (fieldPathElements != null && fieldPathElements.length > 0) {
        firstFieldPathElement = fieldPathElements[0];
      }
      if (firstFieldPathElement != null
          && "operations".equals(firstFieldPathElement.getField())
          && firstFieldPathElement.getIndex() != null) {
        int operationIndex = firstFieldPathElement.getIndex();
        AdGroupCriterion adGroupCriterion = operations.get(operationIndex).getOperand();
        System.out.printf(
            "Ad group criterion with ad group ID %d and keyword '%s' "
                + "triggered a failure for the following reason: %s.%n",
            adGroupCriterion.getAdGroupId(),
            ((Keyword) adGroupCriterion.getCriterion()).getText(),
            apiError.getErrorString());
      } else {
        System.out.printf(
            "A failure has occurred for the following reason: %s%n", apiError.getErrorString());
      }
    }
  }
}

Handle policy violation errors

// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package adwords.axis.v201708.errorhandling;

import com.beust.jcommander.Parameter;
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
import com.google.api.ads.adwords.axis.v201708.cm.AdGroupAd;
import com.google.api.ads.adwords.axis.v201708.cm.AdGroupAdOperation;
import com.google.api.ads.adwords.axis.v201708.cm.AdGroupAdReturnValue;
import com.google.api.ads.adwords.axis.v201708.cm.AdGroupAdServiceInterface;
import com.google.api.ads.adwords.axis.v201708.cm.ApiError;
import com.google.api.ads.adwords.axis.v201708.cm.ApiException;
import com.google.api.ads.adwords.axis.v201708.cm.ExemptionRequest;
import com.google.api.ads.adwords.axis.v201708.cm.ExpandedTextAd;
import com.google.api.ads.adwords.axis.v201708.cm.FieldPathElement;
import com.google.api.ads.adwords.axis.v201708.cm.Operator;
import com.google.api.ads.adwords.axis.v201708.cm.PolicyViolationError;
import com.google.api.ads.adwords.lib.client.AdWordsSession;
import com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface;
import com.google.api.ads.adwords.lib.utils.examples.ArgumentNames;
import com.google.api.ads.common.lib.auth.OfflineCredentials;
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
import com.google.api.ads.common.lib.utils.examples.CodeSampleParams;
import com.google.api.client.auth.oauth2.Credential;
import com.google.common.collect.Sets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * This example demonstrates how to handle policy violation errors.
 *
 * <p>Credentials and properties in {@code fromFile()} are pulled from the "ads.properties" file.
 * See README for more info.
 */
public class HandlePolicyViolationError {

  private static class HandlePolicyViolationErrorParams extends CodeSampleParams {
    @Parameter(names = ArgumentNames.AD_GROUP_ID, required = true)
    private Long adGroupId;
  }

  public static void main(String[] args) throws Exception {
    // Generate a refreshable OAuth2 credential.
    Credential oAuth2Credential =
        new OfflineCredentials.Builder()
            .forApi(Api.ADWORDS)
            .fromFile()
            .build()
            .generateCredential();

    // Construct an AdWordsSession.
    AdWordsSession session =
        new AdWordsSession.Builder().fromFile().withOAuth2Credential(oAuth2Credential).build();

    AdWordsServicesInterface adWordsServices = AdWordsServices.getInstance();

    HandlePolicyViolationErrorParams params = new HandlePolicyViolationErrorParams();
    if (!params.parseArguments(args)) {
      // Either pass the required parameters for this example on the command line, or insert them
      // into the code here. See the parameter class definition above for descriptions.
      params.adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
    }

    runExample(adWordsServices, session, params.adGroupId);
  }

  public static void runExample(
      AdWordsServicesInterface adWordsServices, AdWordsSession session, Long adGroupId)
      throws Exception {
    // Enable validateOnly mode.
    session.setValidateOnly(true);

    // Get the AdGroupAdService.
    AdGroupAdServiceInterface adGroupAdService =
        adWordsServices.get(session, AdGroupAdServiceInterface.class);

    // Create text ad that violates an exemptable policy.
    ExpandedTextAd exemptableExpandedTextAd = new ExpandedTextAd();
    exemptableExpandedTextAd.setHeadlinePart1("Mars " + System.currentTimeMillis() + "!!!");
    exemptableExpandedTextAd.setHeadlinePart2("Best space cruise line.");
    exemptableExpandedTextAd.setDescription("Visit the Red Planet in style.");
    exemptableExpandedTextAd.setFinalUrls(new String[] {"http://www.example.com/"});

    // Create ad group ad.
    AdGroupAd exemptableAdGroupAd = new AdGroupAd();
    exemptableAdGroupAd.setAdGroupId(adGroupId);
    exemptableAdGroupAd.setAd(exemptableExpandedTextAd);

    // Create operations.
    AdGroupAdOperation exemptableOperation = new AdGroupAdOperation();
    exemptableOperation.setOperand(exemptableAdGroupAd);
    exemptableOperation.setOperator(Operator.ADD);

    // Create text ad that violates a non-exemptable policy.
    ExpandedTextAd nonExemptableExpandedTextAd = new ExpandedTextAd();
    nonExemptableExpandedTextAd.setHeadlinePart1("Mars Cruise with too long of a headline.");
    nonExemptableExpandedTextAd.setHeadlinePart2("Best space cruise line.");
    nonExemptableExpandedTextAd.setDescription("Visit the Red Planet in style.");
    nonExemptableExpandedTextAd.setFinalUrls(new String[] {"http://www.example.com/"});

    // Create ad group ad.
    AdGroupAd nonExemptableAdGroupAd = new AdGroupAd();
    nonExemptableAdGroupAd.setAdGroupId(adGroupId);
    nonExemptableAdGroupAd.setAd(nonExemptableExpandedTextAd);

    // Create operations.
    AdGroupAdOperation nonExemptableOperation = new AdGroupAdOperation();
    nonExemptableOperation.setOperand(nonExemptableAdGroupAd);
    nonExemptableOperation.setOperator(Operator.ADD);

    AdGroupAdOperation[] operations =
        new AdGroupAdOperation[] {exemptableOperation, nonExemptableOperation};

    // Use a LinkedHashSet to store each operation index to retry only once and in
    // the same order as the original array of operations.
    Set<Integer> operationIndicesToRetry = Sets.newLinkedHashSet();
    try {
      // Validate the ads.
      adGroupAdService.mutate(operations);
    } catch (ApiException e) {
      for (ApiError error : e.getErrors()) {
        // Get the index of the failed operation from the error's field path elements.
        FieldPathElement[] fieldPathElements = error.getFieldPathElements();
        FieldPathElement firstFieldPathElement = null;
        if (fieldPathElements != null && fieldPathElements.length > 0) {
          firstFieldPathElement = fieldPathElements[0];
        }
        if (firstFieldPathElement == null
            || !"operations".equals(firstFieldPathElement.getField())
            || firstFieldPathElement.getIndex() == null) {
          // If the operation index is not present on the first error field path element, then
          // there's no way to determine which operation to remove, so simply throw the exception.
          throw e;
        }
        int operationIndex = firstFieldPathElement.getIndex();
        AdGroupAdOperation operation = operations[operationIndex];
        if (handleApiError(error, operationIndex, operation)) {
          operationIndicesToRetry.add(operationIndex);
        } else {
          System.out.printf(
              "Removing operation with non-exemptable error at index %d.%n", operationIndex);
        }
      }
    }

    // Create a new list of operations consisting of only the operations that should be retried.
    List<AdGroupAdOperation> operationsToRetry = new ArrayList<>();
    for (Integer operationIndexToRetry : operationIndicesToRetry) {
      operationsToRetry.add(operations[operationIndexToRetry]);
    }

    if (!operationsToRetry.isEmpty()) {
      // Disable validateOnly so we can submit the AdGroupAds with exemptions.
      session.setValidateOnly(false);

      // Add ads with exemptions.
      AdGroupAdReturnValue result =
          adGroupAdService.mutate(
              operationsToRetry.toArray(new AdGroupAdOperation[operationsToRetry.size()]));

      // Display ads.
      if (result != null && result.getValue() != null) {
        for (AdGroupAd adGroupAdResult : result.getValue()) {
          ExpandedTextAd expandedTextAd = (ExpandedTextAd) adGroupAdResult.getAd();
          System.out.printf(
              "Ad with ID %d and headline '%s - %s' was added.%n",
              expandedTextAd.getId(),
              expandedTextAd.getHeadlinePart1(),
              expandedTextAd.getHeadlinePart2());
        }
      }
    } else {
      System.out.println("No ads were added.");
    }
  }

  /**
   * Checks the given error and performs the appropriate action based on whether it is an exemptable
   * policy violation error.
   *
   * @param error the error to handle.
   * @param operationIndex the index of the operation that led to the error.
   * @param operation the operation that led to the error.
   * @return true if the error is an exemptable policy violation error.
   */
  private static boolean handleApiError(
      ApiError error, int operationIndex, AdGroupAdOperation operation) {
    // Determine if the operation can be resubmitted with an exemption request.
    boolean isExemptableError = false;
    PolicyViolationError policyViolationError = null;
    if (error instanceof PolicyViolationError) {
      policyViolationError = (PolicyViolationError) error;
      ExpandedTextAd expandedTextAd = (ExpandedTextAd) operation.getOperand().getAd();
      System.out.printf(
          "Ad with headline '%s - %s' violated %s policy '%s'.%n",
          expandedTextAd.getHeadlinePart1(),
          expandedTextAd.getHeadlinePart2(),
          policyViolationError.getIsExemptable() ? "exemptable" : "non-exemptable",
          policyViolationError.getExternalPolicyName());
      isExemptableError = policyViolationError.getIsExemptable();
    }

    if (isExemptableError) {
      // Add exemption request to the operation.
      System.out.printf(
          "Adding exemption request for policy name '%s' on text '%s' to operation at index %d.%n",
          policyViolationError.getKey().getPolicyName(),
          policyViolationError.getKey().getViolatingText(),
          operationIndex);
      ExemptionRequest exemptionRequest = new ExemptionRequest();
      exemptionRequest.setKey(policyViolationError.getKey());

      List<ExemptionRequest> exemptionRequests =
          (operation.getExemptionRequests() == null)
              ? new ArrayList<ExemptionRequest>()
              : new ArrayList<>(Arrays.asList(operation.getExemptionRequests()));
      exemptionRequests.add(exemptionRequest);
      operation.setExemptionRequests(
          exemptionRequests.toArray(new ExemptionRequest[exemptionRequests.size()]));
    }
    return isExemptableError;
  }
}

Send feedback about...

AdWords API
AdWords API
Need help? Visit our support page.