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

The following examples show how to use io.grpc.Status#getDescription() . 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: StatusProto.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example 2
Source File: UnknownStatusDescriptionInterceptor.java    From apicurio-registry with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 3
Source File: Http2Client.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
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());
  }
}
 
Example 4
Source File: BinlogHelper.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 5
Source File: InternalSubchannel.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 6
Source File: AbstractServerStream.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
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());
  }
}
 
Example 7
Source File: SyncEngine.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: GrpcRequestHandler.java    From xio with Apache License 2.0 5 votes vote down vote up
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();
  }
}
 
Example 9
Source File: GoogleCloudStorageGrpcWriteChannel.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 10
Source File: LoggingInterceptor.java    From bazel with Apache License 2.0 5 votes vote down vote up
/** 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();
}
 
Example 11
Source File: Http2Client.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
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());
  }
}
 
Example 12
Source File: BinlogHelper.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 13
Source File: InternalSubchannel.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 14
Source File: AbstractServerStream.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
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());
  }
}
 
Example 15
Source File: ErrorCatchingServerInterceptor.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
private String formatStatus(Status status) {
    return "{code=" + status.getCode()
            + ", description=" + status.getDescription()
            + ", error=" + (status.getCause() == null ? "N/A" : status.getCause().getMessage())
            + '}';
}
 
Example 16
Source File: CommonErrorCatchingServerInterceptor.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
private String formatStatus(Status status) {
    return "{code=" + status.getCode()
            + ", description=" + status.getDescription()
            + ", error=" + (status.getCause() == null ? "N/A" : status.getCause().getMessage())
            + '}';
}