@Override public void run() { listener.onEvent( null, throwable instanceof FirestoreException ? (FirestoreException) throwable : FirestoreException.apiException( new ApiException( throwable, GrpcStatusCode.of(getStatus(throwable).getCode()), false))); } });
@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 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 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); }
@Override public ResponseT get() throws ExecutionException { if (completedSnapshot.getErrorCode().getCode().equals(StatusCode.Code.OK)) { return (ResponseT) completedSnapshot.getResponse(); } throw new ExecutionException( new ApiException(null, completedSnapshot.getErrorCode(), false)); }
@Override public ResponseT get() throws ExecutionException { if (completedSnapshot.getErrorCode().getCode().equals(StatusCode.Code.OK)) { return (ResponseT) completedSnapshot.getResponse(); } throw new ExecutionException( new ApiException(null, completedSnapshot.getErrorCode(), false)); }
@Nullable @Override public UploadMediaItemResponse apply(@Nullable Throwable input) { Optional<String> resumeUrl = Optional.ofNullable(atomicResumeUrl.get()); return UploadMediaItemResponse.newBuilder() .setError( UploadMediaItemResponse.Error.newBuilder() .setResumeUrl(resumeUrl) .setCause( new ApiException( input, getStatusCode(input), resumeUrl.isPresent() /* retryable */)) .build()) .build(); }
@Override public void run() { listener.onEvent( null, throwable instanceof FirestoreException ? (FirestoreException) throwable : FirestoreException.apiException( new ApiException( throwable, GrpcStatusCode.of(getStatus(throwable).getCode()), false))); } });
@Override public Currency apply(OperationSnapshot operationSnapshot) { if (operationSnapshot.getMetadata() == null) { return null; } if (!(operationSnapshot.getMetadata() instanceof Currency)) { String errorMessage = "type mismatch: expected " + Currency.class.getName() + ", found " + operationSnapshot.getMetadata().getClass().getName(); throw new ApiException(errorMessage, null, FakeStatusCode.of(StatusCode.Code.OK), false); } else { return (Currency) operationSnapshot.getMetadata(); } } }
@Test public void testErrorConversion() { for (Code code : Code.values()) { ApiException error = new ApiException("fake message", null, new FakeStatusCode(code), false); Status opencensusStatus = OpencensusTracer.convertErrorToStatus(error); assertThat(opencensusStatus.getDescription()).isEqualTo("fake message"); assertThat(opencensusStatus.getCanonicalCode().toString()).isEqualTo(code.toString()); } } }
@Override public Color apply(OperationSnapshot operationSnapshot) { if (!operationSnapshot.getErrorCode().getCode().equals(StatusCode.Code.OK)) { throw ApiExceptionFactory.createException( "Operation with name \"" + operationSnapshot.getName() + "\" failed with status = " + operationSnapshot.getErrorCode() + " and message = " + operationSnapshot.getErrorMessage(), null, operationSnapshot.getErrorCode(), false); } if (operationSnapshot.getResponse() == null) { return null; } if (!(operationSnapshot.getResponse() instanceof Color)) { String errorMessage = "type mismatch: expected " + Color.class.getName() + ", found " + operationSnapshot.getResponse().getClass().getName(); throw new ApiException(errorMessage, null, FakeStatusCode.of(StatusCode.Code.OK), false); } else { return (Color) operationSnapshot.getResponse(); } } }