/** * Returns a {@link RpcResponse} corresponding to the given {@link Status} generated by the server. */ public static RpcResponse rpcResponse(Status status, @Nullable Object message) { if (status.isOk()) { return RpcResponse.of(message); } else { return RpcResponse.ofFailure(status.asException()); } }
messageFramer.close(); ctx.logBuilder().responseContent(GrpcLogUtil.rpcResponse(newStatus, firstResponse), null); if (newStatus.isOk()) { if (useBlockingTaskExecutor) { ctx.blockingTaskExecutor().execute(this::invokeOnComplete);
@Override public void readRows( ReadRowsRequest request, StreamObserver<ReadRowsResponse> responseObserver) { RpcExpectation expectedRpc = expectations.poll(); currentRequestIndex++; Assert.assertNotNull( "Unexpected request #" + currentRequestIndex + ": " + request.toString(), expectedRpc); Assert.assertEquals( "Expected request #" + currentRequestIndex + " does not match actual request: " + request.toString(), expectedRpc.expectedRequest, request); for (ReadRowsResponse response : expectedRpc.responses) { responseObserver.onNext(response); } if (expectedRpc.statusCode.toStatus().isOk()) { responseObserver.onCompleted(); } else { responseObserver.onError(expectedRpc.statusCode.toStatus().asRuntimeException()); } } }
@Override public void readRows( ReadRowsRequest request, StreamObserver<ReadRowsResponse> responseObserver) { RpcExpectation expectedRpc = expectations.poll(); i++; Truth.assertWithMessage("Unexpected request#" + i + ":" + request.toString()) .that(expectedRpc) .isNotNull(); Truth.assertWithMessage("Unexpected request#" + i) .that(request) .isEqualTo(expectedRpc.getExpectedRequest()); for (ReadRowsResponse response : expectedRpc.responses) { responseObserver.onNext(response); } if (expectedRpc.statusCode.toStatus().isOk()) { responseObserver.onCompleted(); } else { responseObserver.onError(expectedRpc.statusCode.toStatus().asRuntimeException()); } } }
/** * Creates a {@code FailingClientStream} that would fail with the given error. */ public FailingClientStream(Status error, RpcProgress rpcProgress) { Preconditions.checkArgument(!error.isOk(), "error must not be OK"); this.error = error; this.rpcProgress = rpcProgress; }
FailingClientTransport(Status error, RpcProgress rpcProgress) { Preconditions.checkArgument(!error.isOk(), "error must not be OK"); this.error = error; this.rpcProgress = rpcProgress; }
/** * Returns an instance for {@code TRANSIENT_FAILURE}, associated with an error status. */ public static ConnectivityStateInfo forTransientFailure(Status error) { Preconditions.checkArgument(!error.isOk(), "The error status must not be OK"); return new ConnectivityStateInfo(TRANSIENT_FAILURE, error); }
/** * A decision to report a connectivity error to the RPC. If the RPC is {@link * CallOptions#withWaitForReady wait-for-ready}, it will stay buffered. Otherwise, it will fail * with the given error. * * @param error the error status. Must not be OK. * @since 1.2.0 */ public static PickResult withError(Status error) { Preconditions.checkArgument(!error.isOk(), "error status shouldn't be OK"); return new PickResult(null, null, error, false); }
/** * A decision to fail an RPC immediately. This is a final decision and will ignore retry * policy. * * @param status the status with which the RPC will fail. Must not be OK. * @since 1.8.0 */ public static PickResult withDrop(Status status) { Preconditions.checkArgument(!status.isOk(), "drop status shouldn't be OK"); return new PickResult(null, null, status, true); }
@Override public String toString() { if (status.isOk()) { return state.toString(); } return state + "(" + status + ")"; }
/** * Creates a {@code FailingClientStream} that would fail with the given error. */ public FailingClientStream(Status error) { Preconditions.checkArgument(!error.isOk(), "error must not be OK"); this.error = error; }
@Override public String toString() { if (status.isOk()) { return state.toString(); } return state + "(" + status + ")"; }
private HeaderCacheElement(Status errorStatus) { Preconditions.checkArgument(!errorStatus.isOk(), "Error status can't be OK"); this.token = new HeaderToken(errorStatus, null); this.actualExpirationTimeMs = 0; }
CacheState getCacheState() { long now = clock.currentTimeMillis(); if (!token.status.isOk()) { return CacheState.Exception; } else if (actualExpirationTimeMs - TOKEN_EXPIRES_MS <= now) { return CacheState.Expired; } else if (actualExpirationTimeMs - TOKEN_STALENESS_MS <= now) { return CacheState.Stale; } else { return CacheState.Good; } }
@Override public final void cancel(Status reason) { Preconditions.checkArgument(!reason.isOk(), "Should not cancel with OK status"); cancelled = true; abstractClientStreamSink().cancel(reason); }
@Override public void onClose(Status status, Metadata trailers) { if (status.isOk()) { observer.onCompleted(); } else { observer.onError(status.asRuntimeException(trailers)); } }
/** * Returns an instance for {@code TRANSIENT_FAILURE}, associated with an error status. */ public static ConnectivityStateInfo forTransientFailure(Status error) { Preconditions.checkArgument(!error.isOk(), "The error status must not be OK"); return new ConnectivityStateInfo(TRANSIENT_FAILURE, error); }
/** * A decision to fail an RPC immediately. This is a final decision and will ignore retry * policy. * * @param status the status with which the RPC will fail. Must not be OK. * @since 1.8.0 */ public static PickResult withDrop(Status status) { Preconditions.checkArgument(!status.isOk(), "drop status shouldn't be OK"); return new PickResult(null, null, status, true); }
/** * Returns a {@link RpcResponse} corresponding to the given {@link Status} generated by the server. */ public static RpcResponse rpcResponse(Status status, @Nullable Object message) { if (status.isOk()) { return RpcResponse.of(message); } else { return RpcResponse.ofFailure(status.asException()); } }
@Override public void fail(Status status) { checkArgument(!status.isOk(), "Cannot fail with OK status"); checkState(!finalized, "apply() or fail() already called"); finalizeWith(new FailingClientStream(status)); }