Skip to content

Commit d7a8fef

Browse files
committed
Simplify error classification and merge observability integration tests
1 parent d1470dd commit d7a8fef

4 files changed

Lines changed: 195 additions & 603 deletions

File tree

sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/ErrorTypeUtil.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public enum ErrorType {
114114
* @return a low-cardinality string representing the specific error type, or {@code
115115
* ErrorType.INTERNAL.toString()} if the provided error is {@code null} or non-determined.
116116
*/
117+
// Requirement source: go/clo:product-requirements-v1
117118
public static String extractErrorType(@Nullable Throwable error) {
118119
if (error == null) {
119120
// No information about the error; we default to INTERNAL.
@@ -162,12 +163,8 @@ public static String extractErrorType(@Nullable Throwable error) {
162163
private static String extractServerErrorCode(ApiException apiException) {
163164
if (apiException.getStatusCode() != null) {
164165
Object transportCode = apiException.getStatusCode().getTransportCode();
165-
if (transportCode instanceof Integer) {
166-
// HTTP Status Code
166+
if (transportCode != null) {
167167
return String.valueOf(transportCode);
168-
} else if (apiException.getStatusCode().getCode() != null) {
169-
// gRPC Status Code name
170-
return apiException.getStatusCode().getCode().name();
171168
}
172169
}
173170
return null;
@@ -206,7 +203,7 @@ private static String getClientSideError(Throwable error) {
206203
* @return true if the error is a client timeout, false otherwise.
207204
*/
208205
private static boolean isClientTimeout(Throwable e) {
209-
return hasErrorClassInCauseChain(e, CLIENT_TIMEOUT_EXCEPTION_CLASSES);
206+
return hasErrorClass(e, CLIENT_TIMEOUT_EXCEPTION_CLASSES);
210207
}
211208

212209
/**
@@ -217,11 +214,11 @@ private static boolean isClientTimeout(Throwable e) {
217214
* @return true if the error is a client connection error, false otherwise.
218215
*/
219216
private static boolean isClientConnectionError(Throwable e) {
220-
return hasErrorClassInCauseChain(e, CLIENT_CONNECTION_EXCEPTIONS);
217+
return hasErrorClass(e, CLIENT_CONNECTION_EXCEPTIONS);
221218
}
222219

223220
private static boolean isClientAuthenticationError(Throwable e) {
224-
return hasErrorClassInCauseChain(e, AUTHENTICATION_EXCEPTION_CLASSES);
221+
return hasErrorClass(e, AUTHENTICATION_EXCEPTION_CLASSES);
225222
}
226223

227224
/**
@@ -231,16 +228,11 @@ private static boolean isClientAuthenticationError(Throwable e) {
231228
* @param errorClasses A set of class objects to check against.
232229
* @return true if an error from the set is found in the cause chain, false otherwise.
233230
*/
234-
private static boolean hasErrorClassInCauseChain(
235-
Throwable t, Set<Class<? extends Throwable>> errorClasses) {
236-
Throwable current = t;
237-
while (current != null) {
238-
for (Class<? extends Throwable> errorClass : errorClasses) {
239-
if (errorClass.isInstance(current)) {
240-
return true;
241-
}
231+
private static boolean hasErrorClass(Throwable t, Set<Class<? extends Throwable>> errorClasses) {
232+
for (Class<? extends Throwable> errorClass : errorClasses) {
233+
if (errorClass.isInstance(t)) {
234+
return true;
242235
}
243-
current = current.getCause();
244236
}
245237
return false;
246238
}

sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/rpc/ErrorTypeUtilTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,10 @@ void testExtractErrorType_otherNetworkErrors() {
169169
}
170170

171171
@Test
172-
void testExtractErrorType_causeChainTraversal() {
172+
void testExtractErrorType_noCauseChainTraversal() {
173173
Exception root = new ConnectException("refused");
174174
Exception wrapped = new IOException("io fail", root);
175-
assertThat(ErrorTypeUtil.extractErrorType(wrapped))
176-
.isEqualTo(ErrorTypeUtil.ErrorType.CLIENT_CONNECTION_ERROR.toString());
175+
assertThat(ErrorTypeUtil.extractErrorType(wrapped)).isEqualTo("IOException");
177176
}
178177

179178
@Test

0 commit comments

Comments
 (0)