下面列出了io.grpc.Status#fromCodeValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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
}
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));
}
/**
* 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);
}
@Test
public void fromThrowable_shouldReturnNullIfTrailersAreNull() {
Status status = Status.fromCodeValue(0);
assertNull(StatusProto.fromThrowable(status.asRuntimeException()));
assertNull(StatusProto.fromThrowable(status.asException()));
}
@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)));
}
/**
* 转换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);
}
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());
}
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());
}