io.grpc.Status#trailersFromThrowable ( )源码实例Demo

下面列出了io.grpc.Status#trailersFromThrowable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: grpc-nebula-java   文件: ClientCallsTest.java
@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);
  }
}
 
源代码2 项目: grpc-nebula-java   文件: ClientCallsTest.java
@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);
  }
}
 
源代码3 项目: google-ads-java   文件: BaseGoogleAdsException.java
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();
  }
}
 
源代码4 项目: grpc-java   文件: ClientCallsTest.java
@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);
  }
}
 
源代码5 项目: grpc-java   文件: ClientCallsTest.java
@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);
  }
}
 
源代码6 项目: grpc-nebula-java   文件: DetailErrorSample.java
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);
  }
}
 
/**
 * 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;
}
 
源代码8 项目: grpc-java   文件: DetailErrorSample.java
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);
  }
}