Change some error status usages to be consistent with other gRPC implementations.

This commit is contained in:
Xudong Ma 2015-08-12 15:57:02 -07:00
parent 0c7466cdf7
commit ca7587f641
6 changed files with 22 additions and 8 deletions

View File

@ -41,7 +41,9 @@ import io.grpc.ClientInterceptor;
import io.grpc.ClientInterceptors.CheckedForwardingClientCall;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.Status;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
@ -82,8 +84,8 @@ public class ClientAuthInterceptor implements ClientInterceptor {
// metadata map until the next refresh cycle. This will be fixed once
// https://github.com/google/google-auth-library-java/issues/3
// is resolved.
if (lastMetadata == null || lastMetadata != credentials.getRequestMetadata()) {
lastMetadata = credentials.getRequestMetadata();
if (lastMetadata == null || lastMetadata != getRequestMetadata()) {
lastMetadata = getRequestMetadata();
cached = toHeaders(lastMetadata);
}
cachedSaved = cached;
@ -94,6 +96,14 @@ public class ClientAuthInterceptor implements ClientInterceptor {
};
}
private Map<String, List<String>> getRequestMetadata() {
try {
return credentials.getRequestMetadata();
} catch (IOException e) {
throw Status.UNAUTHENTICATED.withCause(e).asRuntimeException();
}
}
private static final Metadata.Headers toHeaders(Map<String, List<String>> metadata) {
Metadata.Headers headers = new Metadata.Headers();
if (metadata != null) {

View File

@ -137,7 +137,7 @@ public class ClientAuthInterceptorTests {
Mockito.verify(listener).onClose(statusCaptor.capture(), isA(Metadata.Trailers.class));
Assert.assertNull(headers.getAll(AUTHORIZATION));
Mockito.verify(call, never()).start(listener, headers);
Assert.assertEquals(Status.Code.UNKNOWN, statusCaptor.getValue().getCode());
Assert.assertEquals(Status.Code.UNAUTHENTICATED, statusCaptor.getValue().getCode());
Assert.assertNotNull(statusCaptor.getValue().getCause());
}

View File

@ -181,7 +181,7 @@ public abstract class Http2ClientStream extends AbstractClientStream<Integer> {
if (status == null) {
status = statusFromHttpStatus(trailers);
if (status == null || status.isOk()) {
status = Status.INTERNAL.withDescription("missing GRPC status in response");
status = Status.UNKNOWN.withDescription("missing GRPC status in response");
} else {
status = status.withDescription(
"missing GRPC status, inferred error from HTTP status code");

View File

@ -39,6 +39,7 @@ import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipException;
import javax.annotation.concurrent.NotThreadSafe;
@ -352,11 +353,14 @@ public class MessageDeframer implements Closeable {
}
if (compression != Compression.GZIP) {
throw new AssertionError("Unknown compression type");
throw Status.INVALID_ARGUMENT.withDescription("Unknown compression type")
.asRuntimeException();
}
try {
return new GZIPInputStream(ReadableBuffers.openStream(nextFrame, true));
} catch (ZipException e) {
throw Status.INTERNAL.withDescription("Decompression failed").asRuntimeException();
} catch (IOException e) {
throw new RuntimeException(e);
}

View File

@ -437,7 +437,7 @@ class OkHttpClientTransport implements ClientTransport {
*/
void onIoException(IOException failureCause) {
log.log(Level.SEVERE, "Transport failed", failureCause);
onGoAway(0, Status.INTERNAL.withCause(failureCause));
onGoAway(0, Status.UNAVAILABLE.withCause(failureCause));
}
/**

View File

@ -1058,7 +1058,7 @@ public class OkHttpClientTransportTest {
// ping failed on error
assertEquals(1, callback.invocationCount);
assertTrue(callback.failureCause instanceof StatusException);
assertEquals(Status.Code.INTERNAL,
assertEquals(Status.Code.UNAVAILABLE,
((StatusException) callback.failureCause).getStatus().getCode());
// now that handler is in terminal state, all future pings fail immediately
@ -1066,7 +1066,7 @@ public class OkHttpClientTransportTest {
clientTransport.ping(callback, MoreExecutors.directExecutor());
assertEquals(1, callback.invocationCount);
assertTrue(callback.failureCause instanceof StatusException);
assertEquals(Status.Code.INTERNAL,
assertEquals(Status.Code.UNAVAILABLE,
((StatusException) callback.failureCause).getStatus().getCode());
}