ApiException gaxException = ApiExceptionFactory.createException( cause, GrpcStatusCode.of(Status.fromThrowable(cause).getCode()), false); logger.log(Level.SEVERE, "terminated streaming with exception", gaxException); notifyFailed(gaxException);
private ApiException create(Throwable throwable, Status.Code statusCode) { boolean canRetry = retryableCodes.contains(GrpcStatusCode.grpcCodeToStatusCode(statusCode)); return ApiExceptionFactory.createException(throwable, GrpcStatusCode.of(statusCode), canRetry); } }
/** Returns the {@link Status.Code} from grpc. */ @Override public StatusCode.Code getCode() { return grpcCodeToStatusCode(getTransportCode()); }
private void testCodes(StatusCode.Code gaxCode, Status.Code grpcCode) { Truth.assertThat(GrpcStatusCode.grpcCodeToStatusCode(grpcCode)).isEqualTo(gaxCode); } }
@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()); } }
private ApiException create(Throwable throwable, Status.Code statusCode) { boolean canRetry = retryableCodes.contains(GrpcStatusCode.grpcCodeToStatusCode(statusCode)); return ApiExceptionFactory.createException(throwable, GrpcStatusCode.of(statusCode), canRetry); } }
/** Returns the {@link Status.Code} from grpc. */ @Override public StatusCode.Code getCode() { return grpcCodeToStatusCode(getTransportCode()); }
@Override public void run() { listener.onEvent( null, throwable instanceof FirestoreException ? (FirestoreException) throwable : FirestoreException.apiException( new ApiException( throwable, GrpcStatusCode.of(getStatus(throwable).getCode()), false))); } });
/** Convert an entry's status from a protobuf to an {@link ApiException}. */ private ApiException createEntryError(com.google.rpc.Status protoStatus) { io.grpc.Status grpcStatus = io.grpc.Status.fromCodeValue(protoStatus.getCode()) .withDescription(protoStatus.getMessage()); StatusCode gaxStatusCode = GrpcStatusCode.of(grpcStatus.getCode()); return ApiExceptionFactory.createException( grpcStatus.asRuntimeException(), gaxStatusCode, retryableCodes.contains(gaxStatusCode.getCode())); }
@Test public void testExistsFalse() { // Setup NotFoundException exception = new NotFoundException("fake error", null, GrpcStatusCode.of(Status.Code.NOT_FOUND), false); Mockito.when(mockGetTableCallable.futureCall(Matchers.any(GetTableRequest.class))) .thenReturn( ApiFutures.<com.google.bigtable.admin.v2.Table>immediateFailedFuture(exception)); // Execute boolean found = adminClient.exists(TABLE_ID); // Verify assertThat(found).isFalse(); } }
@Test public void testExistsFalse() { // Setup NotFoundException exception = new NotFoundException("fake-error", null, GrpcStatusCode.of(Status.Code.NOT_FOUND), false); Mockito.when(mockGetInstanceCallable.futureCall(Matchers.any(GetInstanceRequest.class))) .thenReturn( ApiFutures.<com.google.bigtable.admin.v2.Instance>immediateFailedFuture(exception)); // Execute boolean found = adminClient.exists(INSTANCE_ID); // Verify assertThat(found).isFalse(); } }
@Test public void testIsRetryable() { assertThat( StatusUtil.isRetryable( new ApiException("derp", null, GrpcStatusCode.of(Code.UNAVAILABLE), false))) .isTrue(); assertThat( StatusUtil.isRetryable( new ApiException( "Server shutdownNow invoked", null, GrpcStatusCode.of(Code.UNAVAILABLE), false))) .isFalse(); } }
@Test public void errorIsPropagated() throws Exception { ApiFuture<List<KeyOffset>> result = callable.futureCall("my-table"); Throwable expectedError = new NotFoundException("fake error", null, GrpcStatusCode.of(Code.NOT_FOUND), false); inner.response.setException(expectedError); Throwable actualError = null; try { result.get(1, TimeUnit.SECONDS); } catch (ExecutionException e) { actualError = e.getCause(); } assertThat(actualError).isEqualTo(expectedError); }
private <ReqT, RespT, MetaT> void mockOperationResult( OperationCallable<ReqT, RespT, MetaT> callable, ReqT request, RespT response) { OperationSnapshot operationSnapshot = FakeOperationSnapshot.newBuilder() .setDone(true) .setErrorCode(GrpcStatusCode.of(Code.OK)) .setName("fake-name") .setResponse(response) .build(); OperationFuture<RespT, MetaT> operationFuture = OperationFutures.immediateOperationFuture(operationSnapshot); Mockito.when(callable.futureCall(request)).thenReturn(operationFuture); }
@Test public void splitRpcExceptionTest() throws Exception { BatchedFuture<Void> result1 = BatchedFuture.create(); BatchedRequestIssuer<Void> issuer1 = new BatchedRequestIssuer<>(result1, 1); BatchedFuture<Void> result2 = BatchedFuture.create(); BatchedRequestIssuer<Void> issuer2 = new BatchedRequestIssuer<>(result2, 1); ImmutableList<BatchedRequestIssuer<Void>> issuers = new ImmutableList.Builder<BatchedRequestIssuer<Void>>().add(issuer1).add(issuer2).build(); ApiException serverError = new ApiException(null, GrpcStatusCode.of(Status.Code.UNAVAILABLE), true); descriptor.splitException(serverError, issuers); issuer1.sendResult(); issuer2.sendResult(); Throwable error1 = null; try { result1.get(1, TimeUnit.SECONDS); } catch (ExecutionException t) { error1 = t.getCause(); } assertThat(error1).isSameAs(serverError); Throwable error2 = null; try { result2.get(1, TimeUnit.SECONDS); } catch (ExecutionException t) { error2 = t.getCause(); } assertThat(error2).isSameAs(serverError); }
@Test public void errorIsPropagated() throws Exception { ApiFuture<Row> result = callable.futureCall( ReadModifyWriteRow.create("my-table", "my-key").append("my-family", "", "suffix")); Throwable expectedError = new NotFoundException("fake error", null, GrpcStatusCode.of(Code.NOT_FOUND), false); inner.response.setException(expectedError); Throwable actualError = null; try { result.get(1, TimeUnit.SECONDS); } catch (ExecutionException e) { actualError = e.getCause(); } assertThat(actualError).isEqualTo(expectedError); }
@Test public void bulkMutationsBatchingFailureTest() throws Exception { BulkMutationBatcher batcher = bigtableDataClient.newBulkMutationBatcher(); RowMutation request = RowMutation.create("fake-table", "some-key") .setCell("some-family", "fake-qualifier", "fake-value"); SettableApiFuture<Void> innerResult = SettableApiFuture.create(); Mockito.when(mockBulkMutateRowsBatchingCallable.futureCall(request)).thenReturn(innerResult); ApiException innerError = new ApiException(null, GrpcStatusCode.of(Code.INTERNAL), false); batcher.add(request); innerResult.setException(innerError); Throwable outerError = null; try { batcher.close(Duration.ofMillis(10)); } catch (Throwable t) { outerError = t; } assertThat(outerError).isInstanceOf(BulkMutationFailure.class); }
@Test public void errorIsPropagated() throws Exception { ApiFuture<Boolean> result = callable.futureCall( ConditionalRowMutation.create("my-table", "row-key") .then(Mutation.create().deleteRow())); Throwable expectedError = new NotFoundException("fake error", null, GrpcStatusCode.of(Code.NOT_FOUND), false); inner.response.setException(expectedError); Throwable actualError = null; try { result.get(1, TimeUnit.SECONDS); } catch (ExecutionException e) { actualError = e.getCause(); } assertThat(actualError).isEqualTo(expectedError); }