Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Commit 918ae41

Browse files
authored
fix: pass error message when creating ApiException (#1556)
1 parent cea4282 commit 918ae41

File tree

3 files changed

+32
-49
lines changed

3 files changed

+32
-49
lines changed

gax-grpc/src/test/java/com/google/api/gax/grpc/ProtoOperationTransformersTest.java

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,20 @@
2929
*/
3030
package com.google.api.gax.grpc;
3131

32+
import static org.junit.Assert.assertThrows;
33+
3234
import com.google.api.gax.grpc.ProtoOperationTransformers.MetadataTransformer;
3335
import com.google.api.gax.grpc.ProtoOperationTransformers.ResponseTransformer;
3436
import com.google.api.gax.longrunning.OperationSnapshot;
35-
import com.google.api.gax.rpc.ApiException;
3637
import com.google.api.gax.rpc.UnavailableException;
38+
import com.google.api.gax.rpc.UnknownException;
3739
import com.google.common.truth.Truth;
3840
import com.google.longrunning.Operation;
3941
import com.google.protobuf.Any;
4042
import com.google.rpc.Status;
4143
import com.google.type.Color;
4244
import com.google.type.Money;
4345
import io.grpc.Status.Code;
44-
import org.junit.Assert;
4546
import org.junit.Test;
4647
import org.junit.runner.RunWith;
4748
import org.junit.runners.JUnit4;
@@ -66,14 +67,11 @@ public void testAnyResponseTransformer_exception() {
6667
OperationSnapshot operationSnapshot =
6768
GrpcOperationSnapshot.create(
6869
Operation.newBuilder().setResponse(Any.pack(inputMoney)).setError(status).build());
69-
try {
70-
transformer.apply(operationSnapshot);
71-
Assert.fail("ResponseTransformer should have thrown an exception");
72-
} catch (UnavailableException expected) {
73-
Truth.assertThat(expected)
74-
.hasMessageThat()
75-
.contains("failed with status = GrpcStatusCode{transportCode=UNAVAILABLE}");
76-
}
70+
Exception exception =
71+
assertThrows(UnavailableException.class, () -> transformer.apply(operationSnapshot));
72+
Truth.assertThat(exception)
73+
.hasMessageThat()
74+
.contains("failed with status = GrpcStatusCode{transportCode=UNAVAILABLE}");
7775
}
7876

7977
@Test
@@ -86,12 +84,9 @@ public void testAnyResponseTransformer_mismatchedTypes() {
8684
.setResponse(Any.pack(Color.getDefaultInstance()))
8785
.setError(status)
8886
.build());
89-
try {
90-
transformer.apply(operationSnapshot);
91-
Assert.fail("ResponseTransformer should have thrown an exception");
92-
} catch (ApiException expected) {
93-
Truth.assertThat(expected).hasMessageThat().contains("Failed to unpack object");
94-
}
87+
Exception exception =
88+
assertThrows(UnknownException.class, () -> transformer.apply(operationSnapshot));
89+
Truth.assertThat(exception).hasMessageThat().contains("encountered a problem unpacking it");
9590
}
9691

9792
@Test
@@ -114,11 +109,8 @@ public void testAnyMetadataTransformer_mismatchedTypes() {
114109
.setMetadata(Any.pack(Color.getDefaultInstance()))
115110
.setError(status)
116111
.build());
117-
try {
118-
transformer.apply(operationSnapshot);
119-
Assert.fail("MetadataTransformer should have thrown an exception");
120-
} catch (ApiException expected) {
121-
Truth.assertThat(expected).hasMessageThat().contains("Failed to unpack object");
122-
}
112+
Exception exception =
113+
assertThrows(UnknownException.class, () -> transformer.apply(operationSnapshot));
114+
Truth.assertThat(exception).hasMessageThat().contains("encountered a problem unpacking it");
123115
}
124116
}

gax-httpjson/src/test/java/com/google/api/gax/httpjson/ProtoOperationTransformersTest.java

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,20 @@
2929
*/
3030
package com.google.api.gax.httpjson;
3131

32+
import static org.junit.Assert.assertThrows;
33+
3234
import com.google.api.gax.httpjson.ProtoOperationTransformers.MetadataTransformer;
3335
import com.google.api.gax.httpjson.ProtoOperationTransformers.ResponseTransformer;
3436
import com.google.api.gax.longrunning.OperationSnapshot;
35-
import com.google.api.gax.rpc.ApiException;
3637
import com.google.api.gax.rpc.UnavailableException;
38+
import com.google.api.gax.rpc.UnknownException;
3739
import com.google.common.truth.Truth;
3840
import com.google.longrunning.Operation;
3941
import com.google.protobuf.Any;
4042
import com.google.rpc.Code;
4143
import com.google.rpc.Status;
4244
import com.google.type.Color;
4345
import com.google.type.Money;
44-
import org.junit.Assert;
4546
import org.junit.Test;
4647

4748
public class ProtoOperationTransformersTest {
@@ -94,14 +95,12 @@ public void testAnyResponseTransformer_exception() {
9495
OperationSnapshot operationSnapshot =
9596
HttpJsonOperationSnapshot.create(
9697
Operation.newBuilder().setResponse(Any.pack(inputMoney)).setError(status).build());
97-
try {
98-
transformer.apply(operationSnapshot);
99-
Assert.fail("ResponseTransformer should have thrown an exception");
100-
} catch (UnavailableException expected) {
101-
Truth.assertThat(expected)
102-
.hasMessageThat()
103-
.contains("failed with status = HttpJsonStatusCode{statusCode=UNAVAILABLE}");
104-
}
98+
99+
Exception exception =
100+
assertThrows(UnavailableException.class, () -> transformer.apply(operationSnapshot));
101+
Truth.assertThat(exception)
102+
.hasMessageThat()
103+
.contains("failed with status = HttpJsonStatusCode{statusCode=UNAVAILABLE}");
105104
}
106105

107106
@Test
@@ -114,12 +113,9 @@ public void testAnyResponseTransformer_mismatchedTypes() {
114113
.setResponse(Any.pack(Color.getDefaultInstance()))
115114
.setError(status)
116115
.build());
117-
try {
118-
transformer.apply(operationSnapshot);
119-
Assert.fail("ResponseTransformer should have thrown an exception");
120-
} catch (ApiException expected) {
121-
Truth.assertThat(expected).hasMessageThat().contains("Failed to unpack object");
122-
}
116+
Exception exception =
117+
assertThrows(UnknownException.class, () -> transformer.apply(operationSnapshot));
118+
Truth.assertThat(exception).hasMessageThat().contains("encountered a problem unpacking it");
123119
}
124120

125121
@Test
@@ -142,11 +138,8 @@ public void testAnyMetadataTransformer_mismatchedTypes() {
142138
.setMetadata(Any.pack(Color.getDefaultInstance()))
143139
.setError(status)
144140
.build());
145-
try {
146-
transformer.apply(operationSnapshot);
147-
Assert.fail("MetadataTransformer should have thrown an exception");
148-
} catch (ApiException expected) {
149-
Truth.assertThat(expected).hasMessageThat().contains("Failed to unpack object");
150-
}
141+
Exception exception =
142+
assertThrows(UnknownException.class, () -> transformer.apply(operationSnapshot));
143+
Truth.assertThat(exception).hasMessageThat().contains("encountered a problem unpacking it");
151144
}
152145
}

gax/src/main/java/com/google/api/gax/rpc/ApiExceptionFactory.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ public static ApiException createException(
4343
return new CancelledException(cause, statusCode, retryable);
4444
case NOT_FOUND:
4545
return new NotFoundException(cause, statusCode, retryable);
46-
case UNKNOWN:
47-
return new UnknownException(cause, statusCode, retryable);
4846
case INVALID_ARGUMENT:
4947
return new InvalidArgumentException(cause, statusCode, retryable);
5048
case DEADLINE_EXCEEDED:
@@ -72,6 +70,7 @@ public static ApiException createException(
7270
case UNAUTHENTICATED:
7371
return new UnauthenticatedException(cause, statusCode, retryable);
7472

73+
case UNKNOWN: // Fall through.
7574
default:
7675
return new UnknownException(cause, statusCode, retryable);
7776
}
@@ -84,8 +83,6 @@ public static ApiException createException(
8483
return new CancelledException(message, cause, statusCode, retryable);
8584
case NOT_FOUND:
8685
return new NotFoundException(message, cause, statusCode, retryable);
87-
case UNKNOWN:
88-
return new UnknownException(message, cause, statusCode, retryable);
8986
case INVALID_ARGUMENT:
9087
return new InvalidArgumentException(message, cause, statusCode, retryable);
9188
case DEADLINE_EXCEEDED:
@@ -113,8 +110,9 @@ public static ApiException createException(
113110
case UNAUTHENTICATED:
114111
return new UnauthenticatedException(message, cause, statusCode, retryable);
115112

113+
case UNKNOWN: // Fall through.
116114
default:
117-
return new UnknownException(cause, statusCode, retryable);
115+
return new UnknownException(message, cause, statusCode, retryable);
118116
}
119117
}
120118
}

0 commit comments

Comments
 (0)