Java Code Examples for io.grpc.Status#trailersFromThrowable()

The following examples show how to use io.grpc.Status#trailersFromThrowable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClientCallsTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void unaryFutureCallFailed() throws Exception {
  final AtomicReference<ClientCall.Listener<String>> listener =
      new AtomicReference<ClientCall.Listener<String>>();
  NoopClientCall<Integer, String> call = new NoopClientCall<Integer, String>() {
    @Override
    public void start(io.grpc.ClientCall.Listener<String> responseListener, Metadata headers) {
      listener.set(responseListener);
    }
  };
  Integer req = 2;
  ListenableFuture<String> future = ClientCalls.futureUnaryCall(call, req);
  Metadata trailers = new Metadata();
  listener.get().onClose(Status.INTERNAL, trailers);
  try {
    future.get();
    fail("Should fail");
  } catch (ExecutionException e) {
    Status status = Status.fromThrowable(e);
    assertEquals(Status.INTERNAL, status);
    Metadata metadata = Status.trailersFromThrowable(e);
    assertSame(trailers, metadata);
  }
}
 
Example 2
Source File: ClientCallsTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void blockingResponseStreamFailed() throws Exception {
  final AtomicReference<ClientCall.Listener<String>> listener =
      new AtomicReference<ClientCall.Listener<String>>();
  NoopClientCall<Integer, String> call = new NoopClientCall<Integer, String>() {
    @Override
    public void start(io.grpc.ClientCall.Listener<String> responseListener, Metadata headers) {
      listener.set(responseListener);
    }
  };

  Integer req = 2;
  Iterator<String> iter = ClientCalls.blockingServerStreamingCall(call, req);

  Metadata trailers = new Metadata();
  listener.get().onClose(Status.INTERNAL, trailers);
  try {
    iter.next();
    fail("Should fail");
  } catch (Exception e) {
    Status status = Status.fromThrowable(e);
    assertEquals(Status.INTERNAL, status);
    Metadata metadata = Status.trailersFromThrowable(e);
    assertSame(trailers, metadata);
  }
}
 
Example 3
Source File: BaseGoogleAdsException.java    From google-ads-java with Apache License 2.0 6 votes vote down vote up
public Optional<T> createGoogleAdsException(ApiException source) {
  if (source == null) {
    return Optional.empty();
  }
  Throwable cause = source.getCause();
  if (cause == null) {
    return Optional.empty();
  }
  Metadata metadata = Status.trailersFromThrowable(cause);
  if (metadata == null) {
    return Optional.empty();
  }
  byte[] protoData = metadata.get(getTrailerKey());
  if (protoData == null) {
    return Optional.empty();
  }
  try {
    return Optional.of(createException(source, protoData, metadata));
  } catch (InvalidProtocolBufferException e) {
    logger.error("Failed to decode GoogleAdsFailure", e);
    return Optional.empty();
  }
}
 
Example 4
Source File: ClientCallsTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void unaryFutureCallFailed() throws Exception {
  final AtomicReference<ClientCall.Listener<String>> listener =
      new AtomicReference<>();
  NoopClientCall<Integer, String> call = new NoopClientCall<Integer, String>() {
    @Override
    public void start(io.grpc.ClientCall.Listener<String> responseListener, Metadata headers) {
      listener.set(responseListener);
    }
  };
  Integer req = 2;
  ListenableFuture<String> future = ClientCalls.futureUnaryCall(call, req);
  Metadata trailers = new Metadata();
  listener.get().onClose(Status.INTERNAL, trailers);
  try {
    future.get();
    fail("Should fail");
  } catch (ExecutionException e) {
    Status status = Status.fromThrowable(e);
    assertEquals(Status.INTERNAL, status);
    Metadata metadata = Status.trailersFromThrowable(e);
    assertSame(trailers, metadata);
  }
}
 
Example 5
Source File: ClientCallsTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void blockingResponseStreamFailed() throws Exception {
  final AtomicReference<ClientCall.Listener<String>> listener =
      new AtomicReference<>();
  NoopClientCall<Integer, String> call = new NoopClientCall<Integer, String>() {
    @Override
    public void start(io.grpc.ClientCall.Listener<String> responseListener, Metadata headers) {
      listener.set(responseListener);
    }
  };

  Integer req = 2;
  Iterator<String> iter = ClientCalls.blockingServerStreamingCall(call, req);

  Metadata trailers = new Metadata();
  listener.get().onClose(Status.INTERNAL, trailers);
  try {
    iter.next();
    fail("Should fail");
  } catch (Exception e) {
    Status status = Status.fromThrowable(e);
    assertEquals(Status.INTERNAL, status);
    Metadata metadata = Status.trailersFromThrowable(e);
    assertSame(trailers, metadata);
  }
}
 
Example 6
Source File: DetailErrorSample.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
static void verifyErrorReply(Throwable t) {
  Status status = Status.fromThrowable(t);
  Metadata trailers = Status.trailersFromThrowable(t);
  Verify.verify(status.getCode() == Status.Code.INTERNAL);
  Verify.verify(trailers.containsKey(DEBUG_INFO_TRAILER_KEY));
  Verify.verify(status.getDescription().equals(DEBUG_DESC));
  try {
    Verify.verify(trailers.get(DEBUG_INFO_TRAILER_KEY).equals(DEBUG_INFO));
  } catch (IllegalArgumentException e) {
    throw new VerifyException(e);
  }
}
 
Example 7
Source File: SpannerExceptionUtil.java    From cloud-spanner-r2dbc with Apache License 2.0 5 votes vote down vote up
/**
 * Extracts the retry delay from the Spanner exception if it exists; else returns null.
 */
private static Duration extractRetryDelay(Throwable cause) {
  Metadata trailers = Status.trailersFromThrowable(cause);
  if (trailers != null && trailers.containsKey(KEY_RETRY_INFO)) {
    RetryInfo retryInfo = trailers.get(KEY_RETRY_INFO);
    if (retryInfo.hasRetryDelay()) {
      com.google.protobuf.Duration protobufDuration = retryInfo.getRetryDelay();
      return Duration.ofSeconds(protobufDuration.getSeconds())
          .withNanos(protobufDuration.getNanos());
    }
  }

  return null;
}
 
Example 8
Source File: DetailErrorSample.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
static void verifyErrorReply(Throwable t) {
  Status status = Status.fromThrowable(t);
  Metadata trailers = Status.trailersFromThrowable(t);
  Verify.verify(status.getCode() == Status.Code.INTERNAL);
  Verify.verify(trailers.containsKey(DEBUG_INFO_TRAILER_KEY));
  Verify.verify(status.getDescription().equals(DEBUG_DESC));
  try {
    Verify.verify(trailers.get(DEBUG_INFO_TRAILER_KEY).equals(DEBUG_INFO));
  } catch (IllegalArgumentException e) {
    throw new VerifyException(e);
  }
}