Skip to content
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

Add method to extract all errors of a partial failure status at once #626

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,68 @@ public abstract class AbstractErrorUtils<
GoogleAdsErrorT extends Message,
FieldPathElementT extends Message> {

/**
* Gets a list of all partial failure error messages for a given response.
*
* <p>For example, given the following Failure:
*
* <pre>
* <code>
* 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"
* }
* }
* }
* </code>
* </pre>
*
* Two {@link GoogleAdsErrorT} instances would be returned for operation index 1 and 2.
*
* @param partialFailureStatus a partialFailure status, with the detail list containing {@link
* GoogleAdsFailureT} instances.
* @return a list containing all the {@link GoogleAdsErrorT} instances.
* @throws 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.
*/
public List<GoogleAdsErrorT> getGoogleAdsErrors(Status partialFailureStatus)
throws InvalidProtocolBufferException {
List<GoogleAdsErrorT> result = new ArrayList();
for (Any detail : partialFailureStatus.getDetailsList()) {
GoogleAdsFailureT failure = getGoogleAdsFailure(detail);
result.addAll(getGoogleAdsErrors(failure));
}
return result;
}

/**
* Gets a list of all partial failure error messages for a given response. Operations are indexed
* from 0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.ads.googleads.lib.utils;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import com.google.ads.googleads.lib.test.errors.Int64Value;
import com.google.ads.googleads.lib.test.errors.MockError;
Expand Down Expand Up @@ -44,6 +45,36 @@ public static String[] parameters() {
return new String[] {"operations", "mutate_operations"};
}

@Test
public void getGoogleAdsErrors_includesAll() throws InvalidProtocolBufferException {
MockPath pathIndex =
MockPath.newBuilder()
.setIndex(Int64Value.newBuilder().setValue(0))
.setFieldName(operationsFieldName)
.build();
MockError errorIndex = MockError.newBuilder().addLocation(pathIndex).build();
MockFailure failureIndex = MockFailure.newBuilder().addErrors(errorIndex).build();

MockPath pathNoIndex = MockPath.newBuilder().setFieldName(operationsFieldName).build();
MockError errorNoIndex = MockError.newBuilder().addLocation(pathNoIndex).build();
MockFailure failureNoIndex = MockFailure.newBuilder().addErrors(errorNoIndex).build();

MockPath pathNoOperation = MockPath.newBuilder().setFieldName("someotherfield").build();
MockError errorNoOperation = MockError.newBuilder().addLocation(pathNoOperation).build();
MockFailure failureNoOperation = MockFailure.newBuilder().addErrors(errorNoOperation).build();

Status status = Status.newBuilder()
.addDetails(Any.pack(failureIndex))
.addDetails(Any.pack(failureNoIndex))
.addDetails(Any.pack(failureNoOperation))
.build();
List<MockError> result = impl.getGoogleAdsErrors(status);
assertEquals(3, result.size());
assertEquals(errorIndex, result.get(0));
assertEquals(errorNoIndex, result.get(1));
assertEquals(errorNoOperation, result.get(2));
}

@Test
public void getGoogleAdsErrors_includesWhen_operationSet() throws InvalidProtocolBufferException {
MockPath path =
Expand Down