@Override public synchronized void onCompleted() { maybeReopenStream(new StatusException(Status.fromCode(Code.UNKNOWN))); }
private void destroy(Code code) throws InterruptedException { streamObserverCapture.getValue().onError(new StatusException(io.grpc.Status.fromCode(code))); }
if (!subscription.isEmpty() && !subscription.equals(request.getSubscription())) { responseObserver.onError( new StatusException( Status.fromCode(Code.ABORTED) .withDescription("Can only set one subscription."))); if (request.getStreamAckDeadlineSeconds() == 0) { responseObserver.onError( new StatusException( Status.fromCode(Code.INVALID_ARGUMENT) .withDescription("A stream must be initialized with a ack deadline."))); closeStream(stream); responseObserver.onError( new StatusException( Status.fromCode(Code.ABORTED) .withDescription( closeStream(stream); responseObserver.onError( new StatusException( Status.fromCode(Code.ABORTED) .withDescription("Invalid modify ack deadline request.")));
@Test public void testFailedChannel_recoverableError_channelReopened() throws Exception { int expectedChannelCount = 1; Subscriber subscriber = startSubscriber( getTestSubscriberBuilder(testReceiver) .setSystemExecutorProvider( InstantiatingExecutorProvider.newBuilder().setExecutorThreadCount(1).build())); // Recoverable error fakeSubscriberServiceImpl.sendError(new StatusException(Status.INTERNAL)); assertEquals(1, fakeSubscriberServiceImpl.waitForClosedStreams(1)); assertEquals( expectedChannelCount, fakeSubscriberServiceImpl.waitForOpenedStreams(expectedChannelCount)); subscriber.stopAsync().awaitTerminated(); }
@Test(expected = ExecutionException.class) public void testPublishFailureRetries_nonRetryableFailsImmediately() throws Exception { Publisher publisher = getTestPublisherBuilder() .setExecutorProvider(SINGLE_THREAD_EXECUTOR) .setRetrySettings( Publisher.Builder.DEFAULT_RETRY_SETTINGS .toBuilder() .setTotalTimeout(Duration.ofSeconds(10)) .build()) .setBatchingSettings( Publisher.Builder.DEFAULT_BATCHING_SETTINGS .toBuilder() .setElementCountThreshold(1L) .setDelayThreshold(Duration.ofSeconds(5)) .build()) .build(); // To demonstrate that reaching duration will trigger publish testPublisherServiceImpl.addPublishError(new StatusException(Status.INVALID_ARGUMENT)); ApiFuture<String> publishFuture1 = sendTestMessage(publisher, "A"); try { publishFuture1.get(); } finally { assertTrue(testPublisherServiceImpl.getCapturedRequests().size() >= 1); publisher.shutdown(); publisher.awaitTermination(1, TimeUnit.MINUTES); } }
@Test(expected = IllegalStateException.class) public void testFailedChannel_fatalError_subscriberFails() throws Exception { Subscriber subscriber = startSubscriber( getTestSubscriberBuilder(testReceiver) .setSystemExecutorProvider( InstantiatingExecutorProvider.newBuilder().setExecutorThreadCount(10).build())); // Fatal error fakeSubscriberServiceImpl.sendError(new StatusException(Status.INVALID_ARGUMENT)); try { subscriber.awaitTerminated(); } finally { // The subscriber must finish with an state error because its FAILED status. assertEquals(Subscriber.State.FAILED, subscriber.state()); Throwable t = subscriber.failureCause(); assertTrue(t instanceof ApiException); ApiException ex = (ApiException) (t); assertTrue(ex.getStatusCode() instanceof GrpcStatusCode); GrpcStatusCode grpcCode = (GrpcStatusCode) ex.getStatusCode(); assertEquals(StatusCode.Code.INVALID_ARGUMENT, grpcCode.getCode()); } }
/** * Convert this {@link Status} to an {@link Exception}. Use {@link #fromThrowable} * to recover this {@link Status} instance when the returned exception is in the causal chain. */ public StatusException asException() { return new StatusException(this); }
/** * Convert this {@link Status} to an {@link Exception}. Use {@link #fromThrowable} * to recover this {@link Status} instance when the returned exception is in the causal chain. */ public StatusException asException() { return new StatusException(this); }
/** * Same as {@link #asException()} but includes the provided trailers in the returned exception. */ @ExperimentalApi public StatusException asException(Metadata trailers) { return new StatusException(this, trailers); }
/** * Same as {@link #asException()} but includes the provided trailers in the returned exception. */ @ExperimentalApi("https://github.com/grpc/grpc-java/issues/4683") public StatusException asException(@Nullable Metadata trailers) { return new StatusException(this, trailers); }
@Override public synchronized void onCompleted() { maybeReopenStream(new StatusException(Status.fromCode(Code.UNKNOWN))); }
@Test public void translateException_StatusException_noRetry() throws Exception { Throwable originalException = new StatusException(Status.INVALID_ARGUMENT); GrpcExceptionCallable<Integer, Integer> exceptionCallable = new GrpcExceptionCallable<>( failingCallable(originalException), Collections.<StatusCode.Code>emptySet()); try { exceptionCallable.call(0); fail("Expected exception to be thrown"); } catch (Throwable throwable) { assertInnerExceptionIsInstanceOf( throwable, InvalidArgumentException.class, NOT_RETRYABLE, originalException); } }
@Test public void translateException_StatusException_withRetry() throws Exception { Throwable originalException = new StatusException(Status.UNAVAILABLE); GrpcExceptionCallable<Integer, Integer> exceptionCallable = new GrpcExceptionCallable<>( failingCallable(originalException), Collections.singleton(Code.UNAVAILABLE)); try { exceptionCallable.call(0); fail("Expected exception to be thrown"); } catch (Throwable throwable) { assertInnerExceptionIsInstanceOf( throwable, UnavailableException.class, IS_RETRYABLE, originalException); } }
throw new StatusException(grpcStatus); };
throw new StatusException(grpcStatus); };