Skip to content

Commit 796cf4a

Browse files
committed
feat(gax): add getErrorInfoList to ErrorDetails
1 parent a1b7565 commit 796cf4a

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

gax-java/gax/src/main/java/com/google/api/gax/rpc/ErrorDetails.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import com.google.rpc.RequestInfo;
4545
import com.google.rpc.ResourceInfo;
4646
import com.google.rpc.RetryInfo;
47+
import java.util.ArrayList;
48+
import java.util.Collections;
4749
import java.util.List;
4850
import javax.annotation.Nullable;
4951

@@ -60,6 +62,14 @@ public ErrorInfo getErrorInfo() {
6062
return unpack(ErrorInfo.class);
6163
}
6264

65+
/**
66+
* This returns all occurrences of ErrorInfo. A single error response may contain multiple
67+
* ErrorInfo messages.
68+
*/
69+
public List<ErrorInfo> getErrorInfoList() {
70+
return unpackList(ErrorInfo.class);
71+
}
72+
6373
/**
6474
* Describes when the clients can retry a failed request. Clients could ignore the recommendation
6575
* here or retry when this information is missing from error responses.
@@ -172,4 +182,26 @@ <T extends Message> T unpack(Class<T> errorTypeClazz) {
172182
}
173183
return null;
174184
}
185+
186+
@VisibleForTesting
187+
<T extends Message> List<T> unpackList(Class<T> errorTypeClazz) {
188+
List<Any> rawErrorMessages = getRawErrorMessages();
189+
if (rawErrorMessages == null) {
190+
return Collections.emptyList();
191+
}
192+
List<T> unpackedMessages = new ArrayList<>();
193+
for (Any detail : rawErrorMessages) {
194+
if (detail.is(errorTypeClazz)) {
195+
try {
196+
unpackedMessages.add(detail.unpack(errorTypeClazz));
197+
} catch (InvalidProtocolBufferException e) {
198+
throw new ProtocolBufferParsingException(
199+
String.format(
200+
"Failed to unpack %s from raw error messages", errorTypeClazz.getSimpleName()),
201+
e);
202+
}
203+
}
204+
}
205+
return Collections.unmodifiableList(unpackedMessages);
206+
}
175207
}

gax-java/gax/src/test/java/com/google/api/gax/rpc/ErrorDetailsTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,28 @@ void errorInfo_shouldUnpackErrorInfoProtoMessage() {
188188
Truth.assertThat(errorDetails.getErrorInfo()).isEqualTo(ERROR_INFO);
189189
}
190190

191+
@Test
192+
void unpackList_shouldReturnEmptyListIfRawErrorMessagesIsNull() {
193+
errorDetails = ErrorDetails.builder().setRawErrorMessages(null).build();
194+
Truth.assertThat(errorDetails.unpackList(ErrorInfo.class)).isEmpty();
195+
}
196+
197+
@Test
198+
void getErrorInfoList_shouldUnpackAllErrorInfoProtoMessages() {
199+
ErrorInfo errorInfo2 =
200+
ErrorInfo.newBuilder().setDomain("googleapis.com").setReason("ANOTHER_REASON").build();
201+
202+
errorDetails =
203+
ErrorDetails.builder()
204+
.setRawErrorMessages(
205+
ImmutableList.of(Any.pack(ERROR_INFO), Any.pack(DEBUG_INFO), Any.pack(errorInfo2)))
206+
.build();
207+
208+
Truth.assertThat(errorDetails.getErrorInfoList())
209+
.containsExactly(ERROR_INFO, errorInfo2)
210+
.inOrder();
211+
}
212+
191213
@Test
192214
void retryInfo_shouldUnpackRetryInfoProtoMessage() {
193215
Truth.assertThat(errorDetails.getRetryInfo()).isEqualTo(RETRY_INFO);

0 commit comments

Comments
 (0)