Skip to content
Permalink
Browse files
fix: pass error message when creating ApiException (#1556)
  • Loading branch information
chanseokoh committed Nov 15, 2021
1 parent cea4282 commit 918ae419f84ad5721638ca10eca992333e9f7c3d
@@ -29,19 +29,20 @@
*/
package com.google.api.gax.grpc;

import static org.junit.Assert.assertThrows;

import com.google.api.gax.grpc.ProtoOperationTransformers.MetadataTransformer;
import com.google.api.gax.grpc.ProtoOperationTransformers.ResponseTransformer;
import com.google.api.gax.longrunning.OperationSnapshot;
import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.UnavailableException;
import com.google.api.gax.rpc.UnknownException;
import com.google.common.truth.Truth;
import com.google.longrunning.Operation;
import com.google.protobuf.Any;
import com.google.rpc.Status;
import com.google.type.Color;
import com.google.type.Money;
import io.grpc.Status.Code;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@@ -66,14 +67,11 @@ public void testAnyResponseTransformer_exception() {
OperationSnapshot operationSnapshot =
GrpcOperationSnapshot.create(
Operation.newBuilder().setResponse(Any.pack(inputMoney)).setError(status).build());
try {
transformer.apply(operationSnapshot);
Assert.fail("ResponseTransformer should have thrown an exception");
} catch (UnavailableException expected) {
Truth.assertThat(expected)
.hasMessageThat()
.contains("failed with status = GrpcStatusCode{transportCode=UNAVAILABLE}");
}
Exception exception =
assertThrows(UnavailableException.class, () -> transformer.apply(operationSnapshot));
Truth.assertThat(exception)
.hasMessageThat()
.contains("failed with status = GrpcStatusCode{transportCode=UNAVAILABLE}");
}

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

@Test
@@ -114,11 +109,8 @@ public void testAnyMetadataTransformer_mismatchedTypes() {
.setMetadata(Any.pack(Color.getDefaultInstance()))
.setError(status)
.build());
try {
transformer.apply(operationSnapshot);
Assert.fail("MetadataTransformer should have thrown an exception");
} catch (ApiException expected) {
Truth.assertThat(expected).hasMessageThat().contains("Failed to unpack object");
}
Exception exception =
assertThrows(UnknownException.class, () -> transformer.apply(operationSnapshot));
Truth.assertThat(exception).hasMessageThat().contains("encountered a problem unpacking it");
}
}
@@ -29,19 +29,20 @@
*/
package com.google.api.gax.httpjson;

import static org.junit.Assert.assertThrows;

import com.google.api.gax.httpjson.ProtoOperationTransformers.MetadataTransformer;
import com.google.api.gax.httpjson.ProtoOperationTransformers.ResponseTransformer;
import com.google.api.gax.longrunning.OperationSnapshot;
import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.UnavailableException;
import com.google.api.gax.rpc.UnknownException;
import com.google.common.truth.Truth;
import com.google.longrunning.Operation;
import com.google.protobuf.Any;
import com.google.rpc.Code;
import com.google.rpc.Status;
import com.google.type.Color;
import com.google.type.Money;
import org.junit.Assert;
import org.junit.Test;

public class ProtoOperationTransformersTest {
@@ -94,14 +95,12 @@ public void testAnyResponseTransformer_exception() {
OperationSnapshot operationSnapshot =
HttpJsonOperationSnapshot.create(
Operation.newBuilder().setResponse(Any.pack(inputMoney)).setError(status).build());
try {
transformer.apply(operationSnapshot);
Assert.fail("ResponseTransformer should have thrown an exception");
} catch (UnavailableException expected) {
Truth.assertThat(expected)
.hasMessageThat()
.contains("failed with status = HttpJsonStatusCode{statusCode=UNAVAILABLE}");
}

Exception exception =
assertThrows(UnavailableException.class, () -> transformer.apply(operationSnapshot));
Truth.assertThat(exception)
.hasMessageThat()
.contains("failed with status = HttpJsonStatusCode{statusCode=UNAVAILABLE}");
}

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

@Test
@@ -142,11 +138,8 @@ public void testAnyMetadataTransformer_mismatchedTypes() {
.setMetadata(Any.pack(Color.getDefaultInstance()))
.setError(status)
.build());
try {
transformer.apply(operationSnapshot);
Assert.fail("MetadataTransformer should have thrown an exception");
} catch (ApiException expected) {
Truth.assertThat(expected).hasMessageThat().contains("Failed to unpack object");
}
Exception exception =
assertThrows(UnknownException.class, () -> transformer.apply(operationSnapshot));
Truth.assertThat(exception).hasMessageThat().contains("encountered a problem unpacking it");
}
}
@@ -43,8 +43,6 @@ public static ApiException createException(
return new CancelledException(cause, statusCode, retryable);
case NOT_FOUND:
return new NotFoundException(cause, statusCode, retryable);
case UNKNOWN:
return new UnknownException(cause, statusCode, retryable);
case INVALID_ARGUMENT:
return new InvalidArgumentException(cause, statusCode, retryable);
case DEADLINE_EXCEEDED:
@@ -72,6 +70,7 @@ public static ApiException createException(
case UNAUTHENTICATED:
return new UnauthenticatedException(cause, statusCode, retryable);

case UNKNOWN: // Fall through.
default:
return new UnknownException(cause, statusCode, retryable);
}
@@ -84,8 +83,6 @@ public static ApiException createException(
return new CancelledException(message, cause, statusCode, retryable);
case NOT_FOUND:
return new NotFoundException(message, cause, statusCode, retryable);
case UNKNOWN:
return new UnknownException(message, cause, statusCode, retryable);
case INVALID_ARGUMENT:
return new InvalidArgumentException(message, cause, statusCode, retryable);
case DEADLINE_EXCEEDED:
@@ -113,8 +110,9 @@ public static ApiException createException(
case UNAUTHENTICATED:
return new UnauthenticatedException(message, cause, statusCode, retryable);

case UNKNOWN: // Fall through.
default:
return new UnknownException(cause, statusCode, retryable);
return new UnknownException(message, cause, statusCode, retryable);
}
}
}

0 comments on commit 918ae41

Please sign in to comment.