io.grpc.Status.getCode()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(100)

本文整理了Java中io.grpc.Status.getCode()方法的一些代码示例,展示了Status.getCode()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Status.getCode()方法的具体详情如下:
包路径:io.grpc.Status
类名称:Status
方法名:getCode

Status.getCode介绍

[英]The canonical status code.
[中]规范状态代码。

代码示例

代码示例来源:origin: AxonFramework/AxonFramework

@Override
public void onError(Throwable ex) {
  logger.warn("Received error from server: {}", ex.getMessage());
  subscriberStreamObserver = null;
  if (ex instanceof StatusRuntimeException && ((StatusRuntimeException) ex).getStatus().getCode().equals(
      Status.UNAVAILABLE.getCode())) {
    return;
  }
  resubscribe();
}

代码示例来源:origin: AxonFramework/AxonFramework

@Override
public void onError(Throwable ex) {
  logger.warn("Received error from server: {}", ex.getMessage());
  outboundStreamObserver = null;
  if (ex instanceof StatusRuntimeException && ((StatusRuntimeException) ex).getStatus().getCode().equals(Status.UNAVAILABLE.getCode())) {
    return;
  }
  resubscribe();
}

代码示例来源:origin: openzipkin/brave

/**
  * Override to change what data from the status or trailers are parsed into the span modeling it.
  * By default, this tags "grpc.status_code" and "error" when it is not OK.
  */
 protected void onClose(Status status, Metadata trailers, SpanCustomizer span) {
  if (status != null && status.getCode() != Status.Code.OK) {
   String code = String.valueOf(status.getCode());
   span.tag("grpc.status_code", code);
   span.tag("error", code);
  }
 }
}

代码示例来源:origin: googleapis/google-cloud-java

/**
 * Returns the error code corresponding to a gRPC status, or {@code UNKNOWN} if not recognized.
 */
static ErrorCode fromGrpcStatus(Status status) {
 ErrorCode code = errorByRpcCode.get(status.getCode().value());
 return code == null ? UNKNOWN : code;
}

代码示例来源:origin: AxonFramework/AxonFramework

@Override
public void onError(Throwable throwable) {
  logger.debug("Lost instruction stream from {} - {}", name, throwable.getMessage());
  disconnectListeners.forEach(Runnable::run);
  if( throwable instanceof StatusRuntimeException) {
    StatusRuntimeException sre = (StatusRuntimeException)throwable;
    if( sre.getStatus().getCode().equals(Status.Code.PERMISSION_DENIED)) return;
  }
  scheduleReconnect(true);
}

代码示例来源:origin: AxonFramework/AxonFramework

private void checkConnectionException(Throwable ex) {
  if (ex instanceof StatusRuntimeException && ((StatusRuntimeException) ex).getStatus().getCode().equals(Status.UNAVAILABLE.getCode())) {
    stopChannelToEventStore();
  }
}

代码示例来源:origin: googleapis/google-cloud-java

/** Determines whether we need to initiate a longer backoff due to system overload. */
 private static boolean isResourceExhaustedError(Throwable throwable) {
  return getStatus(throwable).getCode().equals(Code.RESOURCE_EXHAUSTED);
 };
}

代码示例来源:origin: AxonFramework/AxonFramework

private boolean isDeadlineExceeded(Throwable throwable) {
  return throwable instanceof StatusRuntimeException && ((StatusRuntimeException) throwable).getStatus().getCode().equals(Status.Code.DEADLINE_EXCEEDED);
}

代码示例来源:origin: googleapis/google-cloud-java

@Override
 public void run() {
  listener.onEvent(
    null,
    throwable instanceof FirestoreException
      ? (FirestoreException) throwable
      : FirestoreException.apiException(
        new ApiException(
          throwable,
          GrpcStatusCode.of(getStatus(throwable).getCode()),
          false)));
 }
});

代码示例来源:origin: line/armeria

private static void assertResponseTimeoutExceptionOrDeadlineExceeded(@Nullable Throwable cause) {
  assertThat(cause).isNotNull();
  // Both exceptions can be raised when a timeout occurs due to timing issues,
  // and the first triggered one wins.
  if (cause instanceof ResponseTimeoutException) {
    return;
  }
  if (cause instanceof StatusException) {
    assertThat(((StatusException) cause).getStatus().getCode()).isEqualTo(Code.DEADLINE_EXCEEDED);
    return;
  }
  fail("cause must be a ResponseTimeoutException or a StatusException: ", cause);
}

代码示例来源:origin: googleapis/google-cloud-java

private void awaitException(Code expectedCode) throws InterruptedException {
 FirestoreException exception = exceptions.take();
 if (expectedCode != null) {
  assertEquals(expectedCode, exception.getStatus().getCode());
 }
}

代码示例来源:origin: line/armeria

@Test
  public void httpNotOk() {
    reader.onSubscribe(subscription);
    verifyZeroInteractions(subscription);
    reader.onNext(HttpHeaders.of(HttpStatus.UNAUTHORIZED));
    verifyZeroInteractions(subscription);
    verifyZeroInteractions(deframer);

    verify(transportStatusListener).transportReportStatus(
        argThat(s -> s.getCode() == Status.UNAUTHENTICATED.getCode()));
  }
}

代码示例来源:origin: line/armeria

@Test
public void tooLargeRequest_uncompressed() throws Exception {
  final SimpleRequest request = newLargeRequest();
  final StatusRuntimeException t =
      (StatusRuntimeException) catchThrowable(
          () -> blockingClient.staticUnaryCall(request));
  assertThat(t.getStatus().getCode()).isEqualTo(Code.CANCELLED);
  checkRequestLogStatus(grpcStatus -> {
    assertThat(grpcStatus.getCode()).isEqualTo(Code.RESOURCE_EXHAUSTED);
  });
}

代码示例来源:origin: line/armeria

/** Sends an rpc to an unimplemented method within TestService. */
@Test
public void unimplementedMethod() throws Exception {
  final Throwable t = catchThrowable(() -> blockingStub.unimplementedCall(Empty.getDefaultInstance()));
  assertThat(t).isInstanceOf(StatusRuntimeException.class);
  assertThat(((StatusRuntimeException) t).getStatus().getCode())
      .isEqualTo(Status.UNIMPLEMENTED.getCode());
  checkRequestLogError((headers, rpcReq, cause) -> {
    assertThat(rpcReq).isNotNull();
    assertThat(rpcReq.params()).containsExactly(Empty.getDefaultInstance());
    assertThat(headers.get(GrpcHeaderNames.GRPC_STATUS)).isEqualTo(
        String.valueOf(Status.UNIMPLEMENTED.getCode().value()));
  });
}

代码示例来源:origin: line/armeria

@Test
public void error_withMessage() throws Exception {
  final StatusRuntimeException t = (StatusRuntimeException) catchThrowable(
      () -> blockingClient.errorWithMessage(REQUEST_MESSAGE));
  assertThat(t.getStatus().getCode()).isEqualTo(Code.ABORTED);
  assertThat(t.getStatus().getDescription()).isEqualTo("aborted call");
  checkRequestLog((rpcReq, rpcRes, grpcStatus) -> {
    assertThat(rpcReq.method()).isEqualTo("armeria.grpc.testing.UnitTestService/ErrorWithMessage");
    assertThat(rpcReq.params()).containsExactly(REQUEST_MESSAGE);
    assertThat(grpcStatus).isNotNull();
    assertThat(grpcStatus.getCode()).isEqualTo(Code.ABORTED);
    assertThat(grpcStatus.getDescription()).isEqualTo("aborted call");
  });
}

代码示例来源:origin: line/armeria

@Test
public void error_thrown_unary() throws Exception {
  final StatusRuntimeException t = (StatusRuntimeException) catchThrowable(
      () -> blockingClient.unaryThrowsError(REQUEST_MESSAGE));
  assertThat(t.getStatus().getCode()).isEqualTo(Code.ABORTED);
  assertThat(t.getStatus().getDescription()).isEqualTo("call aborted");
  checkRequestLog((rpcReq, rpcRes, grpcStatus) -> {
    assertThat(rpcReq.method()).isEqualTo("armeria.grpc.testing.UnitTestService/UnaryThrowsError");
    assertThat(rpcReq.params()).containsExactly(REQUEST_MESSAGE);
    assertThat(grpcStatus).isNotNull();
    assertThat(grpcStatus.getCode()).isEqualTo(Code.ABORTED);
    assertThat(grpcStatus.getDescription()).isEqualTo("call aborted");
  });
}

代码示例来源:origin: line/armeria

@Test
public void tooLargeRequest_compressed() throws Exception {
  final SimpleRequest request = newLargeRequest();
  final StatusRuntimeException t =
      (StatusRuntimeException) catchThrowable(
          () -> blockingClient.withCompression("gzip").staticUnaryCall(request));
  assertThat(t.getStatus().getCode()).isEqualTo(Code.CANCELLED);
  checkRequestLogStatus(grpcStatus -> {
    assertThat(grpcStatus.getCode()).isEqualTo(Code.RESOURCE_EXHAUSTED);
  });
}

代码示例来源:origin: line/armeria

@Test
public void error_thrown_streamStub() throws Exception {
  final StreamRecorder<SimpleResponse> response = StreamRecorder.create();
  streamingClient.streamThrowsErrorInStub(response);
  response.awaitCompletion();
  final StatusRuntimeException t = (StatusRuntimeException) response.getError();
  assertThat(t.getStatus().getCode()).isEqualTo(Code.ABORTED);
  assertThat(t.getStatus().getDescription()).isEqualTo("bad streaming stub");
  checkRequestLogStatus(grpcStatus -> {
    assertThat(grpcStatus.getCode()).isEqualTo(Code.ABORTED);
    assertThat(grpcStatus.getDescription()).isEqualTo("bad streaming stub");
  });
}

代码示例来源:origin: line/armeria

@Test
public void error_noMessage() throws Exception {
  final StatusRuntimeException t = (StatusRuntimeException) catchThrowable(
      () -> blockingClient.errorNoMessage(REQUEST_MESSAGE));
  assertThat(t.getStatus().getCode()).isEqualTo(Code.ABORTED);
  assertThat(t.getStatus().getDescription()).isNull();
  checkRequestLog((rpcReq, rpcRes, grpcStatus) -> {
    assertThat(rpcReq.method()).isEqualTo("armeria.grpc.testing.UnitTestService/ErrorNoMessage");
    assertThat(rpcReq.params()).containsExactly(REQUEST_MESSAGE);
    assertThat(grpcStatus).isNotNull();
    assertThat(grpcStatus.getCode()).isEqualTo(Code.ABORTED);
    assertThat(grpcStatus.getDescription()).isNull();
  });
}

代码示例来源:origin: googleapis/google-cloud-java

/** Convert an entry's status from a protobuf to an {@link ApiException}. */
private ApiException createEntryError(com.google.rpc.Status protoStatus) {
 io.grpc.Status grpcStatus =
   io.grpc.Status.fromCodeValue(protoStatus.getCode())
     .withDescription(protoStatus.getMessage());
 StatusCode gaxStatusCode = GrpcStatusCode.of(grpcStatus.getCode());
 return ApiExceptionFactory.createException(
   grpcStatus.asRuntimeException(),
   gaxStatusCode,
   retryableCodes.contains(gaxStatusCode.getCode()));
}

相关文章