4444import com .google .rpc .RequestInfo ;
4545import com .google .rpc .ResourceInfo ;
4646import com .google .rpc .RetryInfo ;
47+ import java .util .ArrayList ;
48+ import java .util .Collections ;
4749import java .util .List ;
4850import 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}
0 commit comments