@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); }
@Override public V apply(ApiException exception) { if (returnNullOnSet.contains(exception.getStatusCode().getCode())) { return null; } throw new LoggingException(exception); } });
public static boolean isRetryable(Throwable error) { if (!(error instanceof ApiException)) { return true; } ApiException apiException = (ApiException) error; switch (apiException.getStatusCode().getCode()) { case DEADLINE_EXCEEDED: case INTERNAL: case CANCELLED: case RESOURCE_EXHAUSTED: case ABORTED: return true; case UNAVAILABLE: // TODO(pongad): check that ApiException propagate message properly. return !apiException.getMessage().contains("Server shutdownNow invoked"); default: return false; } } }
public static Status statusFromApiException(ApiException exception) { if (exception.getStatusCode().getTransportCode() instanceof io.grpc.Status) { io.grpc.Status grpcStatus = (io.grpc.Status) exception.getStatusCode().getTransportCode(); return StatusConverter.fromGrpcStatus(grpcStatus); } return Status.UNKNOWN.withDescription(exception.getMessage()); } }
/** * 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); } }
@SuppressWarnings("ConstantConditions") private void verifyError(ApiFuture<?> result, StatusCode.Code expectedCode) { Throwable error = null; try { result.get(FLUSH_PERIOD.plus(DELAY_BUFFER).toMillis(), TimeUnit.MILLISECONDS); } catch (ExecutionException e) { error = e.getCause(); } catch (Throwable t) { error = t; } assertThat(error).isInstanceOf(ApiException.class); assertThat(((ApiException) error).getStatusCode().getCode()).isEqualTo(expectedCode); }
@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().getStatusCode().getCode()) .isEqualTo(Code.INVALID_ARGUMENT); assertThat(failedMutations.get(1).getError().isRetryable()).isFalse();
@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()); } }
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()); } }
} catch (ApiException e) { System.out.print(e.getStatusCode().getCode()); System.out.print(e.isRetryable());
/** * Returns the status code of the underlying exception. In cases where the underlying status code * can't be determined, the status code will be FakeStatusCode.Code.UNKNOWN. */ public FakeStatusCode getStatusCode() { return (FakeStatusCode) super.getStatusCode(); } }
@Override public V apply(ApiException exception) { if (returnNullOnSet.contains(exception.getStatusCode().getCode())) { return null; } throw new LoggingException(exception); } });
public static Status statusFromApiException(ApiException exception) { if (exception.getStatusCode().getTransportCode() instanceof io.grpc.Status) { io.grpc.Status grpcStatus = (io.grpc.Status) exception.getStatusCode().getTransportCode(); return StatusConverter.fromGrpcStatus(grpcStatus); } return Status.UNKNOWN.withDescription(exception.getMessage()); } }
private RetryableProvisionException handleApiException(ApiException e) throws RetryableProvisionException { if (e.getStatusCode().getCode().getHttpStatusCode() / 100 != 4) { throw new RetryableProvisionException(e); } throw e; }
@InternalApi("Visible for testing") static Status convertErrorToStatus(Throwable error) { if (!(error instanceof ApiException)) { return Status.UNKNOWN.withDescription(error.getMessage()); } ApiException apiException = (ApiException) error; Status.CanonicalCode code; try { code = Status.CanonicalCode.valueOf(apiException.getStatusCode().getCode().name()); } catch (IllegalArgumentException e) { code = CanonicalCode.UNKNOWN; } return code.toStatus().withDescription(error.getMessage()); } }
@InternalApi("Visible for testing") static Status convertErrorToStatus(Throwable error) { if (!(error instanceof ApiException)) { return Status.UNKNOWN.withDescription(error.getMessage()); } ApiException apiException = (ApiException) error; Status.CanonicalCode code; try { code = Status.CanonicalCode.valueOf(apiException.getStatusCode().getCode().name()); } catch (IllegalArgumentException e) { code = CanonicalCode.UNKNOWN; } return code.toStatus().withDescription(error.getMessage()); } }
@Test public void testOnResponseError() throws Throwable { CountDownLatch latch = new CountDownLatch(1); MoneyObserver moneyObserver = new MoneyObserver(true, latch); streamingCallable.call(ERROR_REQUEST, moneyObserver); latch.await(500, TimeUnit.MILLISECONDS); Truth.assertThat(moneyObserver.error).isInstanceOf(ApiException.class); Truth.assertThat(((ApiException) moneyObserver.error).getStatusCode().getCode()) .isEqualTo(StatusCode.Code.INVALID_ARGUMENT); Truth.assertThat(moneyObserver.error) .hasMessageThat() .isEqualTo("io.grpc.StatusRuntimeException: INVALID_ARGUMENT: red must be positive"); }
@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()); } }