public GrpcStatusExceptionImpl(String message, Status status, Throwable cause) { super(message, cause); this.status = status.augmentDescription(message); }
@Override public void run() { handleStreamClosed(Status.fromThrowable(error) .augmentDescription("Stream to GRPCLB LoadBalancer had an error")); } });
@Override public Status getGrpcStatus() { return Status.INVALID_ARGUMENT.augmentDescription(getMessage()); } }
Http2Error(int code, Status status) { this.code = code; this.status = status.augmentDescription("HTTP/2 error code: " + this.name()); }
Http2Error(int code, Status status) { this.code = code; this.status = status.augmentDescription("HTTP/2 error code: " + this.name()); }
private Status mapBrokerErrorToStatus(BrokerError error) { switch (error.getCode()) { case WORKFLOW_NOT_FOUND: return Status.NOT_FOUND.augmentDescription(error.getMessage()); default: return Status.INTERNAL.augmentDescription( String.format( "Unexpected error occurred between gateway and broker (code: %s)", error.getCode())); } }
private Status mapRejectionToStatus(BrokerRejection rejection) { final String description = String.format( "Command rejected with code '%s': %s", rejection.getIntent(), rejection.getReason()); final Status status; switch (rejection.getType()) { case INVALID_ARGUMENT: status = Status.INVALID_ARGUMENT; break; case NOT_FOUND: status = Status.NOT_FOUND; break; case ALREADY_EXISTS: status = Status.ALREADY_EXISTS; break; case INVALID_STATE: status = Status.FAILED_PRECONDITION; break; default: status = Status.UNKNOWN; break; } return status.augmentDescription(description); } }
@Override public void customException(EchoRequest request, StreamObserver<EchoResponse> responseObserver) { try { throw new CustomException("Custom exception!"); } catch (Exception e) { responseObserver.onError(Status.INTERNAL .withDescription(e.getMessage()) .augmentDescription("customException()") .withCause(e) // This can be attached to the Status locally, but NOT transmitted to the client! .asRuntimeException()); } }
private StatusRuntimeException convertThrowable(Throwable cause) { Status status = Status.INTERNAL; if (cause instanceof ExecutionException) { return convertThrowable(cause.getCause()); } if (cause instanceof BrokerErrorException) { status = mapBrokerErrorToStatus(((BrokerErrorException) cause).getError()); } else if (cause instanceof BrokerRejectionException) { status = mapRejectionToStatus(((BrokerRejectionException) cause).getRejection()); } else if (cause instanceof ClientOutOfMemoryException) { status = Status.UNAVAILABLE.augmentDescription(cause.getMessage()); } else if (cause instanceof GrpcStatusException) { status = ((GrpcStatusException) cause).getGrpcStatus(); } else { status = status.augmentDescription("Unexpected error occurred during the request processing"); } final StatusRuntimeException convertedThrowable = status.withCause(cause).asRuntimeException(); Loggers.GATEWAY_LOGGER.error("Error handling gRPC request", convertedThrowable); return convertedThrowable; }
/** * Called by subclasses for the terminal trailer metadata on a stream. * * @param trailers the received terminal trailer metadata */ protected void transportTrailersReceived(Metadata trailers) { Preconditions.checkNotNull(trailers, "trailers"); if (transportError == null && !headersReceived) { transportError = validateInitialMetadata(trailers); if (transportError != null) { transportErrorMetadata = trailers; } } if (transportError != null) { transportError = transportError.augmentDescription("trailers: " + trailers); http2ProcessingFailed(transportError, false, transportErrorMetadata); } else { Status status = statusFromTrailers(trailers); stripTransportDetails(trailers); inboundTrailersReceived(trailers, status); } }
/** * Inspect initial headers to make sure they conform to HTTP and gRPC, returning a {@code Status} * on failure. * * @return status with description of failure, or {@code null} when valid */ @Nullable private Status validateInitialMetadata(Metadata headers) { Integer httpStatus = headers.get(HTTP2_STATUS); if (httpStatus == null) { return Status.INTERNAL.withDescription("Missing HTTP status code"); } String contentType = headers.get(GrpcUtil.CONTENT_TYPE_KEY); if (!GrpcUtil.isGrpcContentType(contentType)) { return GrpcUtil.httpStatusToGrpcStatus(httpStatus) .augmentDescription("invalid content-type: " + contentType); } return null; }
if (transportError != null) { transportError = transportError.augmentDescription("headers: " + headers); return; transportError = transportError.augmentDescription("headers: " + headers); transportErrorMetadata = headers; errorCharset = extractCharset(headers);
/** * Inspect initial headers to make sure they conform to HTTP and gRPC, returning a {@code Status} * on failure. * * @return status with description of failure, or {@code null} when valid */ @Nullable private Status validateInitialMetadata(Metadata headers) { Integer httpStatus = headers.get(HTTP2_STATUS); if (httpStatus == null) { return Status.INTERNAL.withDescription("Missing HTTP status code"); } String contentType = headers.get(GrpcUtil.CONTENT_TYPE_KEY); if (!GrpcUtil.isGrpcContentType(contentType)) { return GrpcUtil.httpStatusToGrpcStatus(httpStatus) .augmentDescription("invalid content-type: " + contentType); } return null; }
if (transportError != null) { transportError = transportError.augmentDescription("headers: " + headers); return; transportError = transportError.augmentDescription("headers: " + headers); transportErrorMetadata = headers; errorCharset = extractCharset(headers);
private static StatusRuntimeException toException(Status status) { io.grpc.Status grpcStatus = io.grpc.Status .fromCodeValue(status.getCode()) .withDescription(status.getMessage()); for (Any detail : status.getDetailsList()) { grpcStatus = grpcStatus.augmentDescription(detail.toString()); } return grpcStatus.asRuntimeException(); }
/** * Called by subclasses for the terminal trailer metadata on a stream. * * @param trailers the received terminal trailer metadata */ protected void transportTrailersReceived(Metadata trailers) { Preconditions.checkNotNull(trailers, "trailers"); if (transportError == null && !headersReceived) { transportError = validateInitialMetadata(trailers); if (transportError != null) { transportErrorMetadata = trailers; } } if (transportError != null) { transportError = transportError.augmentDescription("trailers: " + trailers); http2ProcessingFailed(transportError, false, transportErrorMetadata); } else { Status status = statusFromTrailers(trailers); stripTransportDetails(trailers); inboundTrailersReceived(trailers, status); } }
/** * Extract the response status from trailers. */ private Status statusFromTrailers(Metadata trailers) { Status status = trailers.get(InternalStatus.CODE_KEY); if (status != null) { return status.withDescription(trailers.get(InternalStatus.MESSAGE_KEY)); } // No status; something is broken. Try to provide a resonanable error. if (headersReceived) { return Status.UNKNOWN.withDescription("missing GRPC status in response"); } Integer httpStatus = trailers.get(HTTP2_STATUS); if (httpStatus != null) { status = GrpcUtil.httpStatusToGrpcStatus(httpStatus); } else { status = Status.INTERNAL.withDescription("missing HTTP status code"); } return status.augmentDescription( "missing GRPC status, inferred error from HTTP status code"); }
/** * Extract the response status from trailers. */ private Status statusFromTrailers(Metadata trailers) { Status status = trailers.get(InternalStatus.CODE_KEY); if (status != null) { return status.withDescription(trailers.get(InternalStatus.MESSAGE_KEY)); } // No status; something is broken. Try to provide a resonanable error. if (headersReceived) { return Status.UNKNOWN.withDescription("missing GRPC status in response"); } Integer httpStatus = trailers.get(HTTP2_STATUS); if (httpStatus != null) { status = GrpcUtil.httpStatusToGrpcStatus(httpStatus); } else { status = Status.INTERNAL.withDescription("missing HTTP status code"); } return status.augmentDescription( "missing GRPC status, inferred error from HTTP status code"); }
transportError = transportError.augmentDescription("DATA-----------------------------\n" + ReadableBuffers.readAsString(frame, errorCharset)); frame.close();
transportError = transportError.augmentDescription("DATA-----------------------------\n" + ReadableBuffers.readAsString(frame, errorCharset)); frame.close();