下面列出了io.grpc.Status#getDescription ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Extracts the {@code google.rpc.Status} from trailers, and makes sure they match the gRPC
* {@code status}. If the trailers do not contain a {@code google.rpc.Status}, it uses
* {@code status} param to generate a {@code google.rpc.Status}.
*
* @return the embedded google.rpc.Status
* @since 1.11.0
*/
public static com.google.rpc.Status fromStatusAndTrailers(
Status status, @Nullable Metadata trailers) {
checkNotNull(status, "status");
if (trailers != null) {
com.google.rpc.Status statusProto = trailers.get(STATUS_DETAILS_KEY);
if (statusProto != null) {
checkArgument(
status.getCode().value() == statusProto.getCode(),
"com.google.rpc.Status code must match gRPC status code");
return statusProto;
}
}
// fall-back to status, this is useful if the error is local. e.g. Server is unavailable.
com.google.rpc.Status.Builder statusBuilder = com.google.rpc.Status.newBuilder()
.setCode(status.getCode().value());
if (status.getDescription() != null) {
statusBuilder.setMessage(status.getDescription());
}
return statusBuilder.build();
}
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
ServerCall<ReqT, RespT> wrappedCall = new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
@Override
public void sendMessage(RespT message) {
super.sendMessage(message);
}
@Override
public void close(Status status, Metadata trailers) {
Throwable exception;
Status newStatus;
if (
status.getCode() == Status.Code.UNKNOWN
&& status.getDescription() == null
&& (exception = status.getCause()) != null
&& (newStatus = statusForException(exception)) != null
) {
status = newStatus
.withCause(exception)
.withDescription(stacktraceToString(exception));
}
super.close(status, trailers);
}
};
return next.startCall(wrappedCall, headers);
}
private void assertRstStreamReceived(Status status) {
if (!status.getCode().equals(Status.Code.UNAVAILABLE)) {
throw new AssertionError("Wrong status code. Expected: " + Status.Code.UNAVAILABLE
+ " Received: " + status.getCode());
}
String http2ErrorPrefix = "HTTP/2 error code: NO_ERROR";
if (status.getDescription() == null
|| !status.getDescription().startsWith(http2ErrorPrefix)) {
throw new AssertionError("Wrong HTTP/2 error code. Expected: " + http2ErrorPrefix
+ " Received: " + status.getDescription());
}
}
@Override
void logTrailer(
long seq,
Status status,
Metadata metadata,
GrpcLogEntry.Logger logger,
long callId,
// null on server, can be non null on client if this is a trailer-only response
@Nullable SocketAddress peerAddress) {
Preconditions.checkArgument(
peerAddress == null || logger == GrpcLogEntry.Logger.LOGGER_CLIENT,
"peerSocket can only be specified for client");
MaybeTruncated<io.grpc.binarylog.v1.Metadata.Builder> pair
= createMetadataProto(metadata, maxHeaderBytes);
io.grpc.binarylog.v1.Trailer.Builder trailerBuilder
= io.grpc.binarylog.v1.Trailer.newBuilder()
.setStatusCode(status.getCode().value())
.setMetadata(pair.proto);
String statusDescription = status.getDescription();
if (statusDescription != null) {
trailerBuilder.setStatusMessage(statusDescription);
}
byte[] statusDetailBytes = metadata.get(STATUS_DETAILS_KEY);
if (statusDetailBytes != null) {
trailerBuilder.setStatusDetails(ByteString.copyFrom(statusDetailBytes));
}
GrpcLogEntry.Builder entryBuilder = newTimestampedBuilder()
.setSequenceIdWithinCall(seq)
.setType(EventType.EVENT_TYPE_SERVER_TRAILER)
.setTrailer(trailerBuilder)
.setPayloadTruncated(pair.truncated)
.setLogger(logger)
.setCallId(callId);
if (peerAddress != null) {
entryBuilder.setPeer(socketToProto(peerAddress));
}
sink.write(entryBuilder.build());
}
private String printShortStatus(Status status) {
StringBuilder buffer = new StringBuilder();
buffer.append(status.getCode());
if (status.getDescription() != null) {
buffer.append("(").append(status.getDescription()).append(")");
}
return buffer.toString();
}
private void addStatusToTrailers(Metadata trailers, Status status) {
trailers.discardAll(InternalStatus.CODE_KEY);
trailers.discardAll(InternalStatus.MESSAGE_KEY);
trailers.put(InternalStatus.CODE_KEY, status);
if (status.getDescription() != null) {
trailers.put(InternalStatus.MESSAGE_KEY, status.getDescription());
}
}
private boolean errorIsInteresting(Status error) {
Status.Code code = error.getCode();
String description = error.getDescription() != null ? error.getDescription() : "";
if (code == Status.Code.FAILED_PRECONDITION && description.contains("requires an index")) {
return true;
} else if (code == Status.Code.PERMISSION_DENIED) {
return true;
}
return false;
}
private void sendResponse(
ChannelHandlerContext ctx, int streamId, ByteBuf grpcResponseBuffer, Status status) {
Headers headers =
new DefaultHeaders().set(HttpHeaderNames.CONTENT_TYPE, GRPC_CONTENT_TYPE_VALUE);
DefaultSegmentedResponse segmentedResponse =
DefaultSegmentedResponse.builder()
.streamId(streamId)
.status(HttpResponseStatus.OK)
.headers(headers)
.build();
ctx.writeAndFlush(segmentedResponse);
Headers trailingHeaders =
new DefaultHeaders()
.set(GRPC_TRAILING_HEADER_STATUS_KEY, Integer.toString(status.getCode().value()));
if (status.getDescription() != null) {
trailingHeaders.add(
GRPC_TRAILING_HEADER_MESSAGE_KEY, grpcEncodedString(status.getDescription()));
}
DefaultSegmentedData data =
DefaultSegmentedData.builder()
.streamId(streamId)
.content(grpcResponseBuffer)
.trailingHeaders(trailingHeaders)
.endOfMessage(true)
.build();
ctx.writeAndFlush(data);
HashMap<Integer, GrpcState> session = lazyCreateSession(ctx);
session.remove(streamId);
if (status != Status.OK) {
ctx.close();
}
}
@Override
public void onError(Throwable t) {
Status s = Status.fromThrowable(t);
String statusDesc = s == null ? "" : s.getDescription();
error =
new IOException(
String.format(
"Caught exception for '%s', while uploading to uploadId %s at writeOffset %d."
+ " Status: %s",
resourceId, uploadId, writeOffset, statusDesc),
t);
done.countDown();
}
/** Converts io.grpc.Status to com.google.rpc.Status proto for logging. */
private static com.google.rpc.Status makeStatusProto(Status status) {
String message = "";
if (status.getCause() != null) {
message = status.getCause().toString();
} else if (status.getDescription() != null) {
message = status.getDescription();
}
return com.google.rpc.Status.newBuilder()
.setCode(status.getCode().value())
.setMessage(message)
.build();
}
private void assertRstStreamReceived(Status status) {
if (!status.getCode().equals(Status.Code.UNAVAILABLE)) {
throw new AssertionError("Wrong status code. Expected: " + Status.Code.UNAVAILABLE
+ " Received: " + status.getCode());
}
String http2ErrorPrefix = "HTTP/2 error code: NO_ERROR";
if (status.getDescription() == null
|| !status.getDescription().startsWith(http2ErrorPrefix)) {
throw new AssertionError("Wrong HTTP/2 error code. Expected: " + http2ErrorPrefix
+ " Received: " + status.getDescription());
}
}
@Override
void logTrailer(
long seq,
Status status,
Metadata metadata,
GrpcLogEntry.Logger logger,
long callId,
// null on server, can be non null on client if this is a trailer-only response
@Nullable SocketAddress peerAddress) {
Preconditions.checkArgument(
peerAddress == null || logger == GrpcLogEntry.Logger.LOGGER_CLIENT,
"peerSocket can only be specified for client");
MaybeTruncated<io.grpc.binarylog.v1.Metadata.Builder> pair
= createMetadataProto(metadata, maxHeaderBytes);
io.grpc.binarylog.v1.Trailer.Builder trailerBuilder
= io.grpc.binarylog.v1.Trailer.newBuilder()
.setStatusCode(status.getCode().value())
.setMetadata(pair.proto);
String statusDescription = status.getDescription();
if (statusDescription != null) {
trailerBuilder.setStatusMessage(statusDescription);
}
byte[] statusDetailBytes = metadata.get(STATUS_DETAILS_KEY);
if (statusDetailBytes != null) {
trailerBuilder.setStatusDetails(ByteString.copyFrom(statusDetailBytes));
}
GrpcLogEntry.Builder entryBuilder = newTimestampedBuilder()
.setSequenceIdWithinCall(seq)
.setType(EventType.EVENT_TYPE_SERVER_TRAILER)
.setTrailer(trailerBuilder)
.setPayloadTruncated(pair.truncated)
.setLogger(logger)
.setCallId(callId);
if (peerAddress != null) {
entryBuilder.setPeer(socketToProto(peerAddress));
}
sink.write(entryBuilder.build());
}
private String printShortStatus(Status status) {
StringBuilder buffer = new StringBuilder();
buffer.append(status.getCode());
if (status.getDescription() != null) {
buffer.append("(").append(status.getDescription()).append(")");
}
return buffer.toString();
}
private void addStatusToTrailers(Metadata trailers, Status status) {
trailers.discardAll(InternalStatus.CODE_KEY);
trailers.discardAll(InternalStatus.MESSAGE_KEY);
trailers.put(InternalStatus.CODE_KEY, status);
if (status.getDescription() != null) {
trailers.put(InternalStatus.MESSAGE_KEY, status.getDescription());
}
}
private String formatStatus(Status status) {
return "{code=" + status.getCode()
+ ", description=" + status.getDescription()
+ ", error=" + (status.getCause() == null ? "N/A" : status.getCause().getMessage())
+ '}';
}
private String formatStatus(Status status) {
return "{code=" + status.getCode()
+ ", description=" + status.getDescription()
+ ", error=" + (status.getCause() == null ? "N/A" : status.getCause().getMessage())
+ '}';
}