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

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

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

Status.withDescription介绍

[英]Create a derived instance of Status with the given description. Leading and trailing whitespace may be removed; this may change in the future.
[中]使用给定的描述创建Status的派生实例。可以删除前导和尾随空格;这在未来可能会改变。

代码示例

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

private void verifySize() {
    if (count > maxMessageSize) {
      throw Status.RESOURCE_EXHAUSTED.withDescription(String.format(
          "%s: Compressed frame exceeds maximum frame size: %d. Bytes read: %d. ",
          debugString, maxMessageSize, count)).asRuntimeException();
    }
  }
}

代码示例来源:origin: weibocom/motan

@Override
public void onError(Throwable t) {
  Metadata metadata = Status.trailersFromThrowable(t);
  if (metadata == null) {
    metadata = new Metadata();
  }
  if (t instanceof MotanBizException) {
    call.close(Status.INTERNAL.withDescription(t.getMessage()).withCause(t), metadata);
  } else {
    call.close(Status.UNAVAILABLE.withDescription(t.getMessage()).withCause(t), metadata);
  }
}

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

/**
 * Maps HTTP error response status codes to transport codes, as defined in <a
 * href="https://github.com/grpc/grpc/blob/master/doc/http-grpc-status-mapping.md">
 * http-grpc-status-mapping.md</a>. Never returns a status for which {@code status.isOk()} is
 * {@code true}.
 *
 * <p>Copied from <a href="https://github.com/grpc/grpc-java/blob/master/core/src/main/java/io/grpc/internal/GrpcUtil.java">
 * GrpcUtil.java</a>
 */
public static Status httpStatusToGrpcStatus(int httpStatusCode) {
  return httpStatusToGrpcCode(httpStatusCode).toStatus()
                        .withDescription("HTTP status code " + httpStatusCode);
}

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

private void doCancel(@Nullable String message, @Nullable Throwable cause) {
  if (message == null && cause == null) {
    cause = new CancellationException("Cancelled without a message or cause");
    logger.warn("Cancelling without a message or cause is suboptimal", cause);
  }
  if (cancelCalled) {
    return;
  }
  cancelCalled = true;
  Status status = Status.CANCELLED;
  if (message != null) {
    status = status.withDescription(message);
  }
  if (cause != null) {
    status = status.withCause(cause);
  }
  close(status);
  req.abort();
}

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

@Override
public StreamObserver<SimpleRequest> streamThrowsErrorInStub(
    StreamObserver<SimpleResponse> responseObserver) {
  throw Status.ABORTED.withDescription("bad streaming stub").asRuntimeException();
}

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

@Override
public void unaryThrowsError(SimpleRequest request, StreamObserver<SimpleResponse> responseObserver) {
  throw Status.ABORTED.withDescription("call aborted").asRuntimeException();
}

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

@Override
public void onNext(SimpleRequest value) {
  throw Status.ABORTED.withDescription("bad streaming message").asRuntimeException();
}

代码示例来源:origin: apache/avro

@Override
public Object[] parse(InputStream stream) {
 try {
  BinaryDecoder in = DECODER_FACTORY.binaryDecoder(stream, null);
  Schema reqSchema = message.getRequest();
  GenericRecord request = (GenericRecord) new SpecificDatumReader<>(reqSchema).read(null, in);
  Object[] args = new Object[reqSchema.getFields().size()];
  int i = 0;
  for (Schema.Field field : reqSchema.getFields()) {
   args[i++] = request.get(field.name());
  }
  return args;
 } catch (IOException e) {
  throw Status.INTERNAL.withCause(e).
    withDescription("Error deserializing avro request arguments").asRuntimeException();
 } finally {
  AvroGrpcUtils.skipAndCloseQuietly(stream);
 }
}

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

private ByteBufOrStream getCompressedBody(ByteBuf buf) {
  if (decompressor == Codec.Identity.NONE) {
    buf.release();
    throw Status.INTERNAL.withDescription(
        DEBUG_STRING + ": Can't decode compressed frame as compression not configured.")
               .asRuntimeException();
  }
  try {
    // Enforce the maxMessageSizeBytes limit on the returned stream.
    final InputStream unlimitedStream =
        decompressor.decompress(new ByteBufInputStream(buf, true));
    return new ByteBufOrStream(
        new SizeEnforcingInputStream(unlimitedStream, maxMessageSizeBytes, DEBUG_STRING));
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

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

@Override
public void errorWithMessage(SimpleRequest request, StreamObserver<SimpleResponse> responseObserver) {
  responseObserver.onError(Status.ABORTED.withDescription("aborted call").asException());
}

代码示例来源:origin: Netflix/conductor

@Override
public void batchPoll(TaskServicePb.BatchPollRequest req, StreamObserver<TaskPb.Task> response) {
  final int count = GRPC_HELPER.optionalOr(req.getCount(), 1);
  final int timeout = GRPC_HELPER.optionalOr(req.getTimeout(), POLL_TIMEOUT_MS);
  if (timeout > MAX_POLL_TIMEOUT_MS) {
    response.onError(Status.INVALID_ARGUMENT
        .withDescription("longpoll timeout cannot be longer than " + MAX_POLL_TIMEOUT_MS + "ms")
        .asRuntimeException()
    );
    return;
  }
  try {
    List<Task> polledTasks = taskService.batchPoll(req.getTaskType(), req.getWorkerId(),
        GRPC_HELPER.optional(req.getDomain()), count, timeout);
    LOGGER.info("polled tasks: "+polledTasks);
    polledTasks.stream().map(PROTO_MAPPER::toProto).forEach(response::onNext);
    response.onCompleted();
  } catch (Exception e) {
    GRPC_HELPER.onError(response, e);
  }
}

代码示例来源: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()));
}

代码示例来源:origin: Netflix/conductor

@Override
public void getTask(MetadataServicePb.GetTaskRequest req, StreamObserver<MetadataServicePb.GetTaskResponse> response) {
  TaskDef def = service.getTaskDef(req.getTaskType());
  if (def != null) {
    TaskDefPb.TaskDef task = PROTO_MAPPER.toProto(def);
    response.onNext(MetadataServicePb.GetTaskResponse.newBuilder()
        .setTask(task)
        .build()
    );
    response.onCompleted();
  } else {
    response.onError(Status.NOT_FOUND
        .withDescription("No such TaskDef found by taskType=" + req.getTaskType())
        .asRuntimeException()
    );
  }
}

代码示例来源:origin: Netflix/conductor

@Override
public void getWorkflow(MetadataServicePb.GetWorkflowRequest req, StreamObserver<MetadataServicePb.GetWorkflowResponse > response) {
  try {
    WorkflowDef workflowDef = service.getWorkflowDef(req.getName(), GRPC_HELPER.optional(req.getVersion()));
    WorkflowDefPb.WorkflowDef workflow = PROTO_MAPPER.toProto(workflowDef);
    response.onNext(MetadataServicePb.GetWorkflowResponse.newBuilder()
        .setWorkflow(workflow)
        .build()
    );
    response.onCompleted();
  } catch (ApplicationException e) {
    // TODO replace this with gRPC exception interceptor.
    response.onError(Status.NOT_FOUND
        .withDescription("No such workflow found by name=" + req.getName())
        .asRuntimeException()
    );
  }
}

代码示例来源:origin: Netflix/conductor

@Override
public void getTask(TaskServicePb.GetTaskRequest req, StreamObserver<TaskServicePb.GetTaskResponse> response) {
  try {
    Task task = taskService.getTask(req.getTaskId());
    if (task == null) {
      response.onError(Status.NOT_FOUND
          .withDescription("No such task found by id="+req.getTaskId())
          .asRuntimeException()
      );
    } else {
      response.onNext(
          TaskServicePb.GetTaskResponse.newBuilder()
          .setTask(PROTO_MAPPER.toProto(task))
          .build()
      );
      response.onCompleted();
    }
  } catch (Exception e) {
    GRPC_HELPER.onError(response, e);
  }
}

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

@Test
public void http2InternalErrorIsRetryable() {
 Status status =
   Status.fromCodeValue(Status.Code.INTERNAL.value())
     .withDescription("HTTP/2 error code: INTERNAL_ERROR");
 SpannerException e =
   SpannerExceptionFactory.newSpannerException(new StatusRuntimeException(status));
 assertThat(e.isRetryable()).isTrue();
}

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

@Test
public void connectionClosedIsRetryable() {
 Status status =
   Status.fromCodeValue(Status.Code.INTERNAL.value())
     .withDescription("Connection closed with unknown cause");
 SpannerException e =
   SpannerExceptionFactory.newSpannerException(new StatusRuntimeException(status));
 assertThat(e.isRetryable()).isTrue();
}

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

@Test
public void resourceExhausted() {
 Status status =
   Status.fromCodeValue(Status.Code.RESOURCE_EXHAUSTED.value())
     .withDescription("Memory pushback");
 SpannerException e =
   SpannerExceptionFactory.newSpannerException(new StatusRuntimeException(status));
 assertThat(e.isRetryable()).isFalse();
}

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

@Override
public void onNext(StreamingOutputCallRequest request) {
  if (request.hasResponseStatus()) {
    dispatcher.cancel();
    responseObserver.onError(Status.fromCodeValue(request.getResponseStatus().getCode())
                    .withDescription(
                        request.getResponseStatus().getMessage())
                    .asRuntimeException());
    return;
  }
  dispatcher.enqueue(toChunkQueue(request));
}

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

@Test
public void resourceExhaustedWithBackoff() {
 Status status =
   Status.fromCodeValue(Status.Code.RESOURCE_EXHAUSTED.value())
     .withDescription("Memory pushback");
 Metadata trailers = new Metadata();
 Metadata.Key<RetryInfo> key = ProtoUtils.keyForProto(RetryInfo.getDefaultInstance());
 RetryInfo retryInfo =
   RetryInfo.newBuilder()
     .setRetryDelay(Duration.newBuilder().setNanos(1000000).setSeconds(1L))
     .build();
 trailers.put(key, retryInfo);
 SpannerException e =
   SpannerExceptionFactory.newSpannerException(new StatusRuntimeException(status, trailers));
 assertThat(e.isRetryable()).isTrue();
 assertThat(e.getRetryDelayInMillis()).isEqualTo(1001);
}

相关文章