@Override public void onError(Throwable t) { Metadata metadata = Status.trailersFromThrowable(t); if (metadata == null) { metadata = new Metadata(); } if (t instanceof MotanBizException) { call.close(Status.INTERNAL.withDescription(t.getMessage()).withCause(t), metadata); } else { call.close(Status.UNAVAILABLE.withDescription(t.getMessage()).withCause(t), metadata); } }
static long extractRetryDelay(Throwable cause) { if (cause != null) { Metadata trailers = Status.trailersFromThrowable(cause); if (trailers != null && trailers.containsKey(KEY_RETRY_INFO)) { RetryInfo retryInfo = trailers.get(KEY_RETRY_INFO); if (retryInfo.hasRetryDelay()) { return Durations.toMillis(retryInfo.getRetryDelay()); } } } return -1L; } }
@Override public void onError(Throwable t) { Metadata metadata = Status.trailersFromThrowable(t); if (metadata == null) { metadata = new Metadata(); } call.close(Status.fromThrowable(t), metadata); }
@Override public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> serverCall, Metadata metadata, ServerCallHandler<ReqT, RespT> serverCallHandler) { AsyncListener<ReqT> asyncListener = new AsyncListener<>(); vertx.<ServerCall.Listener<ReqT>>executeBlocking(blockingCode -> blockingCode.complete(interceptor.interceptCall(serverCall, metadata, serverCallHandler)), false, ar -> { if (ar.succeeded()) { asyncListener.setDelegate(ar.result()); } else { Metadata md = Status.trailersFromThrowable(ar.cause()); if (md == null) { md = new Metadata(); } serverCall.close(Status.fromThrowable(ar.cause()), md); } }); return asyncListener; }
static long extractRetryDelay(Throwable cause) { if (cause != null) { Metadata trailers = Status.trailersFromThrowable(cause); if (trailers != null && trailers.containsKey(KEY_RETRY_INFO)) { RetryInfo retryInfo = trailers.get(KEY_RETRY_INFO); if (retryInfo.hasRetryDelay()) { return Durations.toMillis(retryInfo.getRetryDelay()); } } } return -1L; } }
public static GrpcError throwableToStatus(Throwable t) { Status status = Status.fromThrowable(t); if (t instanceof InvalidProtocolBufferException) { status = Status.INVALID_ARGUMENT.withCause(t); } Metadata trailer = Status.trailersFromThrowable(t); int statusCode = grpcToHttpStatus(status); com.google.rpc.Status.Builder payload = com.google.rpc.Status.newBuilder(); payload.setCode(status.getCode().value()); StringBuilder errorMessage = new StringBuilder( "HTTP " + statusCode + " (gRPC: " + status.getCode().name() + ")"); if (!Strings.isNullOrEmpty(status.getDescription())) { errorMessage.append(": ").append(Strings.nullToEmpty(status.getDescription())); } payload.setMessage(errorMessage.toString()); if (trailer != null) { if (trailer.containsKey(RETRY_INFO_KEY)) { RetryInfo retryInfo = trailer.get(RETRY_INFO_KEY); payload.addDetails(Any.pack(retryInfo)); } if (trailer.containsKey(DEBUG_INFO_KEY)) { DebugInfo debugInfo = trailer.get(DEBUG_INFO_KEY); payload.addDetails(Any.pack(debugInfo)); } } return new GrpcError( status, payload.build() ); }