Class AbstractErrorUtils<GoogleAdsFailureT extends com.google.protobuf.Message,GoogleAdsErrorT extends com.google.protobuf.Message,FieldPathElementT extends com.google.protobuf.Message>

java.lang.Object
com.google.ads.googleads.lib.utils.AbstractErrorUtils<GoogleAdsFailureT,GoogleAdsErrorT,FieldPathElementT>

public abstract class AbstractErrorUtils<GoogleAdsFailureT extends com.google.protobuf.Message,GoogleAdsErrorT extends com.google.protobuf.Message,FieldPathElementT extends com.google.protobuf.Message> extends Object
Contains utility methods for handling partial failure of operations.
  • Constructor Details

    • AbstractErrorUtils

      public AbstractErrorUtils()
  • Method Details

    • getGoogleAdsErrors

      public List<GoogleAdsErrorT> getGoogleAdsErrors(long operationIndex, com.google.rpc.Status partialFailureStatus) throws com.google.protobuf.InvalidProtocolBufferException
      Gets a list of all partial failure error messages for a given response. Operations are indexed from 0.

      For example, given the following Failure:

         
           errors {
             message: "Too low."
             location {
               field_path_elements {
                 field_name: "operations"
                 index {
                   value: 1
                 }
               }
               field_path_elements {
                 field_name: "create"
               }
               field_path_elements {
                 field_name: "campaign"
               }
             }
           }
           errors {
             message: "Too low."
             location {
               field_path_elements {
                 field_name: "operations"
                 index {
                   value: 2
                 }
               }
               field_path_elements {
                 field_name: "create"
               }
               field_path_elements {
                 field_name: "campaign"
               }
             }
           }
         
       
      A single AbstractErrorUtils instance would be returned for operation index 1 and 2, and an empty list otherwise.

      This method supports XXXService.mutate(request) where the request contains a list of operations named "operations". It also supports:

      • GoogleAdsService.mutateGoogleAds(request), where the request contains a list of MutateOperations named "mutate_operations".
      • ConversionAdjustmentUploadService.uploadConversionAdjustments(request), where the request contains a list of ConversionAdjustments named "conversion_adjustments".
      • UploadClickConversionsRequest.uploadClickConversions(request), where the request contains a list of ClickConversions named "conversions".
      • UploadCallConversionsRequest.uploadCallConversions(request), where the request contains a list of CallConversions named "conversions".
      Parameters:
      operationIndex - the index of the operation, starting from 0.
      partialFailureStatus - a partialFailure status, with the detail list containing AbstractErrorUtils instances.
      Returns:
      a list containing the AbstractErrorUtils instances for a given operation index.
      Throws:
      com.google.protobuf.InvalidProtocolBufferException - if not able to unpack the protocol buffer. This is most likely due to using the wrong version of ErrorUtils being used with the API response.
    • getGoogleAdsErrors

      public List<GoogleAdsErrorT> getGoogleAdsErrors(long operationIndex, GoogleAdsFailureT googleAdsFailure)
      Return a list of AbstractErrorUtils instances for a given operation index.
      See Also:
    • getFailedOperationIndices

      public List<Long> getFailedOperationIndices(GoogleAdsFailureT googleAdsFailureT)
      Provides a convenience method to get distinct failed operation indices. Returns a list of Long objects because this method may be used with BatchJobResult objects, which have an operationIndex of type long.

      IMPORTANT: Make sure you use Long objects to check for the existence of an item in this list. For example, the following lookup will return false due to autoboxing, even if the list contains Long.valueOf(25).

      Incorrect approach:

         List failedIndices = getFailedOperationIndices(googleAdsFailure);
         int intOpIndex = 25;
         // The JVM will autobox intOpIndex as an Integer, not a Long, so contains(intOpIndex) will
         // return false. In fact, it will return false for ANY int value because the equals
         // method of Long and Integer returns false if the argument is not a Long or
         // is not an Integer, respectively.
         if (failedIndices.contains(intOpIndex)) {
           // This block will never execute.
           // ... handle error
         }
       

      Correct approach. Casts the int index to long so the JVM will autobox into a Long value:

         List failedIndices = getFailedOperationIndices(googleAdsFailure);
         int intOpIndex = 25;
         // Casts the int value to a long for correctness. This will also work correctly if the
         // argument is Long.valueOf(intOpIndex).
         if (failedIndices.contains((long) intOpIndex)) {
           // This block will execute if failedIndices contains 25.
           // ... handle error
         }
       
    • getGoogleAdsFailure

      public GoogleAdsFailureT getGoogleAdsFailure(com.google.protobuf.Any detail)
      Unpacks a single AbstractErrorUtils from an Any instance.
      Throws:
      AbstractErrorUtils.DeserializeException - if an InvalidProtocolBufferException is encountered. This would indicate that the detail object was not-null, but the contents couldn't be deserialized to the target type. This may indicate that the target type is incorrect, or that the content of the Any message is incorrect.
      NullPointerException - if detail is null.
    • getGoogleAdsFailure

      public GoogleAdsFailureT getGoogleAdsFailure(com.google.rpc.Status partialFailureStatus)
      Unpacks the GoogleAdsFailureT instance form a partial failure status object.

      The status object contains a details repeated field. This contains at most 1 Any protos which encode a GoogleAdsFailure instance.

      Parameters:
      partialFailureStatus - the partial failure Status object returned in the repsponse.
      Returns:
      the GoogleAdsFailure instance describing the partial failures, or null if none is found.
      Throws:
      AbstractErrorUtils.DeserializeException - if an InvalidProtocolBufferException is encountered.
      NullPointerException - if partialFailureStatus is null.
    • isPartialFailureResult

      public boolean isPartialFailureResult(com.google.protobuf.Message message)
      Checks if a result in a mutate response is a partial failure.
    • getErrorPaths

      protected Iterable<AbstractErrorUtils.ErrorPath<GoogleAdsErrorT>> getErrorPaths(GoogleAdsFailureT googleAdsFailure)
      Extracts an AbstractErrorUtils.ErrorPath for each GoogleAdsError in googleAdsFailure.
      Parameters:
      googleAdsFailure - the failure from which to extract AbstractErrorUtils.ErrorPaths.
      Returns:
      all error paths found
    • getFieldPathElements

      public abstract List<FieldPathElementT> getFieldPathElements(GoogleAdsErrorT googleAdsError)
      Extracts the AbstractErrorUtils instances from the AbstractErrorUtils.
      Parameters:
      googleAdsError - the error from which to extract field paths.
      Returns:
      the available field paths.
    • getGoogleAdsErrors

      public abstract List<GoogleAdsErrorT> getGoogleAdsErrors(GoogleAdsFailureT googleAdsFailure)
      Extracts the AbstractErrorUtils instances from AbstractErrorUtils.
      Parameters:
      googleAdsFailure - the failure from which to extract.
      Returns:
      the errors extracted.
    • getGoogleAdsFailureClass

      public abstract Class<GoogleAdsFailureT> getGoogleAdsFailureClass()
      Returns the Class instance for AbstractErrorUtils.
    • createErrorPath

      protected abstract AbstractErrorUtils.ErrorPath<GoogleAdsErrorT> createErrorPath(GoogleAdsErrorT error, FieldPathElementT errorLocation)