@Override public boolean shouldRetry(Throwable prevThrowable, ResponseT prevResponse) { return (prevThrowable instanceof ApiException) && ((ApiException) prevThrowable).isRetryable(); } }
@Override public void onFailure(Throwable throwable) { if (throwable instanceof ApiException) { ApiException apiException = ((ApiException) throwable); // details on the API exception System.out.println(apiException.getStatusCode().getCode()); System.out.println(apiException.isRetryable()); } System.out.println("Error publishing message : " + message); }
/** * Create a synthetic {@link ApiException} for an individual entry. When the entire RPC fails, it * implies that all entries failed as well. This helper is used to make that behavior explicit. * The generated exception will have the overall error as its cause. */ private static ApiException createSyntheticErrorForRpcFailure(Throwable overallRequestError) { if (overallRequestError instanceof ApiException) { ApiException requestApiException = (ApiException) overallRequestError; return ApiExceptionFactory.createException( "Didn't receive a result for this mutation entry", overallRequestError, requestApiException.getStatusCode(), requestApiException.isRetryable()); } return ApiExceptionFactory.createException( "Didn't receive a result for this mutation entry", overallRequestError, LOCAL_UNKNOWN_STATUS, false); } }
/** * Handle an RPC level failure by generating a {@link FailedMutation} for each expected entry. The * newly generated {@link FailedMutation}s will be combined with the permanentFailures to give the * caller the whole picture since the first call. This method will always throw a {@link * MutateRowsException}. */ private void handleAttemptError(Throwable rpcError) { ApiException entryError = createSyntheticErrorForRpcFailure(rpcError); ImmutableList.Builder<FailedMutation> allFailures = ImmutableList.builder(); MutateRowsRequest lastRequest = currentRequest; allFailures.addAll(permanentFailures); Builder builder = lastRequest.toBuilder().clearEntries(); List<Integer> newOriginalIndexes = Lists.newArrayList(); for (int i = 0; i < currentRequest.getEntriesCount(); i++) { int origIndex = getOriginalIndex(i); FailedMutation failedMutation = FailedMutation.create(origIndex, entryError); allFailures.add(failedMutation); if (!failedMutation.getError().isRetryable()) { permanentFailures.add(failedMutation); } else { // Schedule the mutation entry for the next RPC by adding it to the request builder and // recording its original index newOriginalIndexes.add(origIndex); builder.addEntries(lastRequest.getEntries(i)); } } currentRequest = builder.build(); originalIndexes = newOriginalIndexes; throw new MutateRowsException(rpcError, allFailures.build(), entryError.isRetryable()); }
if (!failedMutation.getError().isRetryable()) { permanentFailures.add(failedMutation); } else {
assertThat(failedMutations.get(0).getError().isRetryable()).isFalse(); assertThat(failedMutations.get(0).getError().getCause()).isEqualTo(rpcError); assertThat(failedMutations.get(1).getError().isRetryable()).isFalse(); assertThat(failedMutations.get(1).getError().getCause()).isEqualTo(rpcError);
assertThat(failedMutations.get(0).getError().isRetryable()).isTrue(); assertThat(failedMutations.get(0).getError().getCause()).isEqualTo(rpcError); assertThat(failedMutations.get(1).getError().isRetryable()).isTrue(); assertThat(failedMutations.get(1).getError().getCause()).isEqualTo(rpcError);
@BetaApi public BaseGrpcServiceException(ApiException apiException) { super( ExceptionData.newBuilder() .setMessage(apiException.getMessage()) .setCause(apiException) .setRetryable(apiException.isRetryable()) .setCode(apiException.getStatusCode().getCode().getHttpStatusCode()) .setReason(apiException.getStatusCode().getCode().name()) .setLocation(null) .setDebugInfo(null) .build()); } }
assertThat(failedMutations.get(0).getError().getStatusCode().getCode()) .isEqualTo(Code.UNAVAILABLE); assertThat(failedMutations.get(0).getError().isRetryable()).isTrue(); assertThat(failedMutations.get(1).getError().isRetryable()).isFalse();
assertThat(failedMutations.get(0).getError().getStatusCode().getCode()) .isEqualTo(Code.INVALID_ARGUMENT); assertThat(failedMutations.get(0).getError().isRetryable()).isFalse();
/** * Create a topic. * * @param args topicId * @throws Exception exception thrown if operation is unsuccessful */ public static void main(String... args) throws Exception { // Your Google Cloud Platform project ID String projectId = ServiceOptions.getDefaultProjectId(); // Your topic ID, eg. "my-topic" String topicId = args[0]; // Create a new topic ProjectTopicName topic = ProjectTopicName.of(projectId, topicId); try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { topicAdminClient.createTopic(topic); } catch (ApiException e) { // example : code = ALREADY_EXISTS(409) implies topic already exists System.out.print(e.getStatusCode().getCode()); System.out.print(e.isRetryable()); } System.out.printf("Topic %s:%s created.\n", topic.getProject(), topic.getTopic()); } }
System.out.print(e.isRetryable());
@Override public boolean shouldRetry(Throwable prevThrowable, ResponseT prevResponse) { return (prevThrowable instanceof ApiException) && ((ApiException) prevThrowable).isRetryable(); } }
@Override public boolean shouldRetry(Throwable prevThrowable, ResponseT prevResponse) { return (prevThrowable instanceof ApiException) && ((ApiException) prevThrowable).isRetryable(); } }
@Override public boolean shouldRetry(Throwable prevThrowable, ResponseT prevResponse) { return (prevThrowable instanceof ApiException) && ((ApiException) prevThrowable).isRetryable(); } }
public void assertInnerExceptionIsInstanceOf( Throwable thrownException, Class<?> clazz, boolean retryable, Throwable originalException) throws Exception { Truth.assertThat(thrownException).isInstanceOf(clazz); ApiException apiException = (ApiException) thrownException; Truth.assertThat(apiException.isRetryable()).isEqualTo(retryable); } }
/** * Create a synthetic {@link ApiException} for an individual entry. When the entire RPC fails, it * implies that all entries failed as well. This helper is used to make that behavior explicit. * The generated exception will have the overall error as its cause. */ private static ApiException createSyntheticErrorForRpcFailure(Throwable overallRequestError) { if (overallRequestError instanceof ApiException) { ApiException requestApiException = (ApiException) overallRequestError; return ApiExceptionFactory.createException( "Didn't receive a result for this mutation entry", overallRequestError, requestApiException.getStatusCode(), requestApiException.isRetryable()); } return ApiExceptionFactory.createException( "Didn't receive a result for this mutation entry", overallRequestError, LOCAL_UNKNOWN_STATUS, false); } }
/** * Handle an RPC level failure by generating a {@link FailedMutation} for each expected entry. The * newly generated {@link FailedMutation}s will be combined with the permanentFailures to give the * caller the whole picture since the first call. This method will always throw a {@link * MutateRowsException}. */ private void handleAttemptError(Throwable rpcError) { ApiException entryError = createSyntheticErrorForRpcFailure(rpcError); ImmutableList.Builder<FailedMutation> allFailures = ImmutableList.builder(); MutateRowsRequest lastRequest = currentRequest; allFailures.addAll(permanentFailures); Builder builder = lastRequest.toBuilder().clearEntries(); List<Integer> newOriginalIndexes = Lists.newArrayList(); for (int i = 0; i < currentRequest.getEntriesCount(); i++) { int origIndex = getOriginalIndex(i); FailedMutation failedMutation = FailedMutation.create(origIndex, entryError); allFailures.add(failedMutation); if (!failedMutation.getError().isRetryable()) { permanentFailures.add(failedMutation); } else { // Schedule the mutation entry for the next RPC by adding it to the request builder and // recording its original index newOriginalIndexes.add(origIndex); builder.addEntries(lastRequest.getEntries(i)); } } currentRequest = builder.build(); originalIndexes = newOriginalIndexes; throw new MutateRowsException(rpcError, allFailures.build(), entryError.isRetryable()); }
if (!failedMutation.getError().isRetryable()) { permanentFailures.add(failedMutation); } else {
@BetaApi public BaseGrpcServiceException(ApiException apiException) { super( ExceptionData.newBuilder() .setMessage(apiException.getMessage()) .setCause(apiException) .setRetryable(apiException.isRetryable()) .setCode(apiException.getStatusCode().getCode().getHttpStatusCode()) .setReason(apiException.getStatusCode().getCode().name()) .setLocation(null) .setDebugInfo(null) .build()); } }