/** * Throw a DatastoreException with {@code FAILED_PRECONDITION} reason and the {@code message} in a * nested exception. * * @throws DatastoreException every time */ static DatastoreException throwInvalidRequest(String massage, Object... params) { throw new DatastoreException( UNKNOWN_CODE, String.format(massage, params), "FAILED_PRECONDITION"); }
static DatastoreException propagateUserException(Exception ex) { throw new DatastoreException(BaseServiceException.UNKNOWN_CODE, ex.getMessage(), null, ex); } }
/** * Sets a new named binding. * * @param name name of the binding * @param value a {@link FullEntity} object or a list of {@link FullEntity} objects that binds * to a given name */ @Deprecated public Builder<V> setBinding(String name, FullEntity<?>... value) { throw new DatastoreException( Status.Code.UNIMPLEMENTED.value(), "Binding entities is not supported.", "UNIMPLEMENTED"); }
/** * Sets a new positional binding. * * @param value a {@link FullEntity} object or a list of {@link FullEntity} objects to be set as * a new positional binding */ @Deprecated public Builder<V> addBinding(FullEntity<?>... value) { throw new DatastoreException( Status.Code.UNIMPLEMENTED.value(), "Binding entities is not supported.", "UNIMPLEMENTED"); }
/** * Translate RetryHelperException to the DatastoreException that caused the error. This method * will always throw an exception. * * @throws DatastoreException when {@code ex} was caused by a {@code DatastoreException} */ static DatastoreException translateAndThrow(RetryHelperException ex) { BaseServiceException.translate(ex); throw new DatastoreException(UNKNOWN_CODE, ex.getMessage(), null, ex.getCause()); }
private static DatastoreException translate( com.google.datastore.v1.client.DatastoreException exception, boolean idempotent) { String reason = ""; if (exception.getCode() != null) { reason = exception.getCode().name(); } if (reason.isEmpty()) { if (exception.getCause() instanceof IOException) { return new DatastoreException((IOException) exception.getCause()); } } return new DatastoreException( exception.getCode().getNumber(), exception.getMessage(), reason, idempotent, exception); }
ExceptionHandler transactionHandler = TransactionExceptionHandler.build(); assertFalse(handler.shouldRetry(new DatastoreException(10, "", "ABORTED", false, null), null)); assertFalse(handler.shouldRetry(new DatastoreException(10, "", "", false, null), null)); assertFalse(handler.shouldRetry(new DatastoreException(0, "", "", false, null), null)); new DatastoreException(10, "", "ABORTED", false, null), null)); assertTrue( transactionHandler.shouldRetry(new DatastoreException(10, "", "", false, null), null)); assertFalse( transactionHandler.shouldRetry(new DatastoreException(0, "", "", false, null), null)); new DatastoreException( BaseServiceException.UNKNOWN_CODE, "", null, new DatastoreException(10, "", "ABORTED", false, null)); new DatastoreException( BaseServiceException.UNKNOWN_CODE, "", null, new RuntimeException(""));
@Test public void testNonRetryableException() throws Exception { LookupRequest requestPb = LookupRequest.newBuilder().addKeys(KEY1.toPb()).build(); EasyMock.expect(rpcMock.lookup(requestPb)) .andThrow( new DatastoreException(DatastoreException.UNKNOWN_CODE, "denied", "PERMISSION_DENIED")) .times(1); EasyMock.replay(rpcFactoryMock, rpcMock); Datastore datastore = rpcMockOptions.getService(); thrown.expect(DatastoreException.class); thrown.expectMessage("denied"); datastore.get(KEY1); EasyMock.verify(rpcFactoryMock, rpcMock); }
@Test public void testTranslateAndThrow() throws Exception { Exception cause = new DatastoreException(14, "message", "UNAVAILABLE"); RetryHelper.RetryHelperException exceptionMock = createMock(RetryHelper.RetryHelperException.class);
@Test public void testRetryableException() throws Exception { LookupRequest requestPb = LookupRequest.newBuilder().addKeys(KEY1.toPb()).build(); LookupResponse responsePb = LookupResponse.newBuilder() .addFound(EntityResult.newBuilder().setEntity(ENTITY1.toPb())) .build(); EasyMock.expect(rpcMock.lookup(requestPb)) .andThrow(new DatastoreException(14, "UNAVAILABLE", "UNAVAILABLE", null)) .andReturn(responsePb); EasyMock.replay(rpcFactoryMock, rpcMock); Datastore datastore = rpcMockOptions.getService(); Entity entity = datastore.get(KEY1); assertEquals(ENTITY1, entity); EasyMock.verify(rpcFactoryMock, rpcMock); }
@Test public void testDatastoreException() throws Exception { DatastoreException exception = new DatastoreException(10, "message", "ABORTED"); assertEquals(10, exception.getCode()); assertEquals("ABORTED", exception.getReason()); assertTrue(exception.isRetryable()); exception = new DatastoreException(4, "message", "DEADLINE_EXCEEDED"); assertEquals(4, exception.getCode()); assertEquals("DEADLINE_EXCEEDED", exception.getReason()); assertTrue(exception.isRetryable()); exception = new DatastoreException(14, "message", "UNAVAILABLE"); assertEquals(14, exception.getCode()); assertEquals("UNAVAILABLE", exception.getReason()); exception = new DatastoreException(2, "message", "INTERNAL"); assertEquals(2, exception.getCode()); assertEquals("INTERNAL", exception.getReason()); exception = new DatastoreException(cause); assertEquals(DatastoreException.UNKNOWN_CODE, exception.getCode()); assertNull(exception.getReason()); exception = new DatastoreException(2, "message", "INTERNAL", cause); assertEquals(2, exception.getCode()); assertEquals("INTERNAL", exception.getReason());
throw new DatastoreException(ex.getCode(), "Failed retrying datastore " + ORIGINAL_TRIES + " times ", ex.getReason(), ex);
/** * Throw a DatastoreException with {@code FAILED_PRECONDITION} reason and the {@code message} in a * nested exception. * * @throws DatastoreException every time */ static DatastoreException throwInvalidRequest(String massage, Object... params) { throw new DatastoreException( UNKNOWN_CODE, String.format(massage, params), "FAILED_PRECONDITION"); }
static DatastoreException propagateUserException(Exception ex) { throw new DatastoreException(BaseServiceException.UNKNOWN_CODE, ex.getMessage(), null, ex); } }
/** * Sets a new named binding. * * @param name name of the binding * @param value a {@link FullEntity} object or a list of {@link FullEntity} objects that binds * to a given name */ @Deprecated public Builder<V> setBinding(String name, FullEntity<?>... value) { throw new DatastoreException( Status.Code.UNIMPLEMENTED.value(), "Binding entities is not supported.", "UNIMPLEMENTED"); }
/** * Sets a new positional binding. * * @param value a {@link FullEntity} object or a list of {@link FullEntity} objects to be set as * a new positional binding */ @Deprecated public Builder<V> addBinding(FullEntity<?>... value) { throw new DatastoreException( Status.Code.UNIMPLEMENTED.value(), "Binding entities is not supported.", "UNIMPLEMENTED"); }
/** * Translate RetryHelperException to the DatastoreException that caused the error. This method * will always throw an exception. * * @throws DatastoreException when {@code ex} was caused by a {@code DatastoreException} */ static DatastoreException translateAndThrow(RetryHelperException ex) { BaseServiceException.translate(ex); throw new DatastoreException(UNKNOWN_CODE, ex.getMessage(), null, ex.getCause()); }
private static DatastoreException translate( com.google.datastore.v1.client.DatastoreException exception, boolean idempotent) { String reason = ""; if (exception.getCode() != null) { reason = exception.getCode().name(); } if (reason.isEmpty()) { if (exception.getCause() instanceof IOException) { return new DatastoreException((IOException) exception.getCause()); } } return new DatastoreException( exception.getCode().getNumber(), exception.getMessage(), reason, idempotent, exception); }