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

The following examples show how to use io.grpc.Status#fromCodeValue() . 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: SpecTestCase.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
private void doWatchRemove(JSONObject watchRemoveSpec) throws Exception {
  Status error = null;
  JSONObject cause = watchRemoveSpec.optJSONObject("cause");
  if (cause != null) {
    int code = cause.optInt("code");
    if (code != 0) {
      error = Status.fromCodeValue(code);
    }
  }
  List<Integer> targetIds = parseIntList(watchRemoveSpec.getJSONArray("targetIds"));
  WatchTargetChange change =
      new WatchTargetChange(
          WatchTargetChangeType.Removed, targetIds, WatchStream.EMPTY_RESUME_TOKEN, error);
  writeWatchChange(change, SnapshotVersion.NONE);
  // Unlike web, the MockDatastore detects a watch removal with cause and will remove active
  // targets
}
 
Example 2
Source File: SpecTestCase.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
private void doFailWrite(JSONObject writeFailureSpec) throws Exception {
  JSONObject errorSpec = writeFailureSpec.getJSONObject("error");
  boolean keepInQueue = writeFailureSpec.optBoolean("keepInQueue", false);

  int code = errorSpec.getInt("code");
  Status error = Status.fromCodeValue(code);

  Pair<Mutation, Task<Void>> write = getCurrentOutstandingWrites().get(0);
  validateNextWriteSent(write.first);

  // If this is a permanent error, the write is not expected to be sent again.
  if (!keepInQueue) {
    getCurrentOutstandingWrites().remove(0);
  }

  log("      Failing a write.");
  queue.runSync(() -> datastore.failWrite(error));
}
 
Example 3
Source File: GrpcStatus.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * Extracts the gRPC status from the {@link HttpHeaders}, closing the {@link HttpStreamReader} for a
 * successful response, then delivering the status to the {@link TransportStatusListener}.
 */
public static void reportStatus(HttpHeaders headers,
                                HttpStreamReader reader,
                                TransportStatusListener transportStatusListener) {
    final String grpcStatus = headers.get(GrpcHeaderNames.GRPC_STATUS);
    Status status = Status.fromCodeValue(Integer.valueOf(grpcStatus));
    if (status.getCode() == Status.OK.getCode()) {
        // Successful response, finish delivering messages before returning the status.
        reader.closeDeframer();
    }
    final String grpcMessage = headers.get(GrpcHeaderNames.GRPC_MESSAGE);
    if (grpcMessage != null) {
        status = status.withDescription(StatusMessageEscaper.unescape(grpcMessage));
    }
    final String grpcThrowable = headers.get(GrpcHeaderNames.ARMERIA_GRPC_THROWABLEPROTO_BIN);
    if (grpcThrowable != null) {
        status = addCause(status, grpcThrowable);
    }

    final Metadata metadata = MetadataUtil.copyFromHeaders(headers);

    transportStatusListener.transportReportStatus(status, metadata);
}
 
Example 4
Source File: StatusProtoTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void fromThrowable_shouldReturnNullIfTrailersAreNull() {
  Status status = Status.fromCodeValue(0);

  assertNull(StatusProto.fromThrowable(status.asRuntimeException()));
  assertNull(StatusProto.fromThrowable(status.asException()));
}
 
Example 5
Source File: StatusProtoTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void fromThrowable_shouldReturnNullIfStatusDetailsKeyIsMissing() {
  Status status = Status.fromCodeValue(0);
  Metadata emptyMetadata = new Metadata();

  assertNull(StatusProto.fromThrowable(status.asRuntimeException(emptyMetadata)));
  assertNull(StatusProto.fromThrowable(status.asException(emptyMetadata)));
}
 
Example 6
Source File: GrpcClientHandler.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
/**
 * 转换grpc应答
 *
 * @param channel 通道
 * @param message 消息
 * @return 应答消息
 */
protected Object input(final Channel channel, final Http2ResponseMessage message) throws IOException {
    if (message.getStreamId() <= 0) {
        return null;
    }
    Http2ResponseMessage http2Msg = adjoin(message);
    if (http2Msg == null) {
        return null;
    }
    MessageHeader header = new MessageHeader(serialization.getTypeId(), MsgType.BizResp.getType(), GRPC_NUMBER);
    header.setMsgId(http2Msg.getMsgId());
    header.addAttribute(HeaderMapping.STREAM_ID.getNum(), http2Msg.getStreamId());
    ResponsePayload payload;
    Object grpcStatusVal = http2Msg.getEndHeaders().get(GRPC_STATUS_KEY);
    int grpcStatus = grpcStatusVal == null ? Code.UNKNOWN.value() : Integer.parseInt(grpcStatusVal.toString());
    if (grpcStatus == Code.OK.value()) {
        EnhanceCompletableFuture<Long, Message> future = channel.getFutureManager().get(http2Msg.getMsgId());
        if (future != null) {
            payload = decodePayload(http2Msg, (ClassWrapper) future.getAttr());
        } else {
            payload = new ResponsePayload(new GrpcBizException(String.format("request is timeout. id=%d", http2Msg.getMsgId())));
        }
    } else {
        Status status = Status.fromCodeValue(grpcStatus);
        String errMsg = String.format("%s [%d]: %s", status.getCode().name(), grpcStatus, http2Msg.headers().get(GRPC_MESSAGE_KEY));
        payload = new ResponsePayload(new GrpcBizException(errMsg));
    }
    return new ResponseMessage<>(header, payload);
}
 
Example 7
Source File: StatusProto.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
private static Status toStatus(com.google.rpc.Status statusProto) {
  Status status = Status.fromCodeValue(statusProto.getCode());
  checkArgument(status.getCode().value() == statusProto.getCode(), "invalid status code");
  return status.withDescription(statusProto.getMessage());
}
 
Example 8
Source File: StatusProto.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
private static Status toStatus(com.google.rpc.Status statusProto) {
  Status status = Status.fromCodeValue(statusProto.getCode());
  checkArgument(status.getCode().value() == statusProto.getCode(), "invalid status code");
  return status.withDescription(statusProto.getMessage());
}