Refine search
@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); }
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()); } }
@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 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 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 V apply(ApiException exception) { if (returnNullOnSet.contains(exception.getStatusCode().getCode())) { return null; } throw new LoggingException(exception); } });
@Test @SuppressWarnings("all") public void instantiateWorkflowTemplateExceptionTest2() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockWorkflowTemplateService.addException(exception); try { Map<String, String> parameters = new HashMap<>(); client.instantiateWorkflowTemplateAsync(parameters).get(); Assert.fail("No exception raised"); } catch (ExecutionException e) { Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); InvalidArgumentException apiException = (InvalidArgumentException) e.getCause(); Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); } }
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()); } }
@Test @SuppressWarnings("all") public void asyncBatchAnnotateFilesExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockImageAnnotator.addException(exception); try { List<AsyncAnnotateFileRequest> requests = new ArrayList<>(); client.asyncBatchAnnotateFilesAsync(requests).get(); Assert.fail("No exception raised"); } catch (ExecutionException e) { Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); InvalidArgumentException apiException = (InvalidArgumentException) e.getCause(); Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); } } }
/** 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 @SuppressWarnings("all") public void asyncBatchAnnotateFilesExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockImageAnnotator.addException(exception); try { List<AsyncAnnotateFileRequest> requests = new ArrayList<>(); client.asyncBatchAnnotateFilesAsync(requests).get(); Assert.fail("No exception raised"); } catch (ExecutionException e) { Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); InvalidArgumentException apiException = (InvalidArgumentException) e.getCause(); Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); } } }
@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()); } }
@Test @SuppressWarnings("all") public void asyncBatchAnnotateFilesExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockImageAnnotator.addException(exception); try { List<AsyncAnnotateFileRequest> requests = new ArrayList<>(); client.asyncBatchAnnotateFilesAsync(requests).get(); Assert.fail("No exception raised"); } catch (ExecutionException e) { Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); InvalidArgumentException apiException = (InvalidArgumentException) e.getCause(); Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); } } }
@Test @SuppressWarnings("all") public void annotateVideoExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockVideoIntelligenceService.addException(exception); try { String inputUri = "gs://demomaker/cat.mp4"; Feature featuresElement = Feature.LABEL_DETECTION; List<Feature> features = Arrays.asList(featuresElement); client.annotateVideoAsync(inputUri, features).get(); Assert.fail("No exception raised"); } catch (ExecutionException e) { Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); InvalidArgumentException apiException = (InvalidArgumentException) e.getCause(); Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); } } }
@Test @SuppressWarnings("all") public void annotateVideoExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockVideoIntelligenceService.addException(exception); try { String inputUri = "gs://demomaker/cat.mp4"; Feature featuresElement = Feature.LABEL_DETECTION; List<Feature> features = Arrays.asList(featuresElement); client.annotateVideoAsync(inputUri, features).get(); Assert.fail("No exception raised"); } catch (ExecutionException e) { Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); InvalidArgumentException apiException = (InvalidArgumentException) e.getCause(); Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); } } }
@Test @SuppressWarnings("all") public void annotateVideoExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockVideoIntelligenceService.addException(exception); try { String inputUri = "gs://demomaker/cat.mp4"; Feature featuresElement = Feature.LABEL_DETECTION; List<Feature> features = Arrays.asList(featuresElement); client.annotateVideoAsync(inputUri, features).get(); Assert.fail("No exception raised"); } catch (ExecutionException e) { Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); InvalidArgumentException apiException = (InvalidArgumentException) e.getCause(); Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); } } }
@Test @SuppressWarnings("all") public void batchDeleteEntityTypesExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockEntityTypes.addException(exception); try { ProjectAgentName parent = ProjectAgentName.of("[PROJECT]"); List<String> entityTypeNames = new ArrayList<>(); client.batchDeleteEntityTypesAsync(parent, entityTypeNames).get(); Assert.fail("No exception raised"); } catch (ExecutionException e) { Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); InvalidArgumentException apiException = (InvalidArgumentException) e.getCause(); Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); } }
@Test @SuppressWarnings("all") public void batchCreateEntitiesExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockEntityTypes.addException(exception); try { EntityTypeName parent = EntityTypeName.of("[PROJECT]", "[ENTITY_TYPE]"); List<EntityType.Entity> entities = new ArrayList<>(); client.batchCreateEntitiesAsync(parent, entities).get(); Assert.fail("No exception raised"); } catch (ExecutionException e) { Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); InvalidArgumentException apiException = (InvalidArgumentException) e.getCause(); Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); } }
@Test @SuppressWarnings("all") public void batchUpdateEntitiesExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockEntityTypes.addException(exception); try { EntityTypeName parent = EntityTypeName.of("[PROJECT]", "[ENTITY_TYPE]"); List<EntityType.Entity> entities = new ArrayList<>(); client.batchUpdateEntitiesAsync(parent, entities).get(); Assert.fail("No exception raised"); } catch (ExecutionException e) { Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); InvalidArgumentException apiException = (InvalidArgumentException) e.getCause(); Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); } }