@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); } });
/** 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())); }
@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()); } }
@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()); } }
@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()); } }
@Test @SuppressWarnings("all") public void deleteDatasetExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockAutoMl.addException(exception); try { DatasetName name = DatasetName.of("[PROJECT]", "[LOCATION]", "[DATASET]"); client.deleteDatasetAsync(name).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 trainAgentExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockAgents.addException(exception); try { ProjectName parent = ProjectName.of("[PROJECT]"); client.trainAgentAsync(parent).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 trainAgentExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockAgents.addException(exception); try { ProjectName parent = ProjectName.of("[PROJECT]"); client.trainAgentAsync(parent).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 runAssetDiscoveryExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockSecurityCenter.addException(exception); try { OrganizationName parent = OrganizationName.of("[ORGANIZATION]"); client.runAssetDiscoveryAsync(parent).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 deleteInstanceExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockCloudRedis.addException(exception); try { InstanceName name = InstanceName.of("[PROJECT]", "[LOCATION]", "[INSTANCE]"); client.deleteInstanceAsync(name).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 deleteInstanceExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockCloudRedis.addException(exception); try { InstanceName name = InstanceName.of("[PROJECT]", "[LOCATION]", "[INSTANCE]"); client.deleteInstanceAsync(name).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 createDatabaseExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockDatabaseAdmin.addException(exception); try { InstanceName parent = InstanceName.of("[PROJECT]", "[INSTANCE]"); String createStatement = "createStatement552974828"; client.createDatabaseAsync(parent, createStatement).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 deleteDocumentExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockDocuments.addException(exception); try { DocumentName name = DocumentName.of("[PROJECT]", "[KNOWLEDGE_BASE]", "[DOCUMENT]"); client.deleteDocumentAsync(name).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 exportAgentExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockAgents.addException(exception); try { ProjectName parent = ProjectName.of("[PROJECT]"); client.exportAgentAsync(parent).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 instantiateInlineWorkflowTemplateExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockWorkflowTemplateService.addException(exception); try { String formattedParent = RegionName.format("[PROJECT]", "[REGION]"); WorkflowTemplate template = WorkflowTemplate.newBuilder().build(); client.instantiateInlineWorkflowTemplateAsync(formattedParent, template).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 exportDataExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockAutoMl.addException(exception); try { DatasetName name = DatasetName.of("[PROJECT]", "[LOCATION]", "[DATASET]"); OutputConfig outputConfig = OutputConfig.newBuilder().build(); client.exportDataAsync(name, outputConfig).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()); } }