Search in sources :

Example 1 with RetryHelperException

use of com.google.cloud.RetryHelper.RetryHelperException in project google-cloud-java by GoogleCloudPlatform.

the class StorageImpl method get.

@Override
public Blob get(BlobId blob, BlobGetOption... options) {
    final StorageObject storedObject = blob.toPb();
    final Map<StorageRpc.Option, ?> optionsMap = optionMap(blob, options);
    try {
        StorageObject storageObject = runWithRetries(new Callable<StorageObject>() {

            @Override
            public StorageObject call() {
                return storageRpc.get(storedObject, optionsMap);
            }
        }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock());
        return storageObject == null ? null : Blob.fromPb(this, storageObject);
    } catch (RetryHelperException e) {
        throw StorageException.translateAndThrow(e);
    }
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException)

Example 2 with RetryHelperException

use of com.google.cloud.RetryHelper.RetryHelperException in project google-cloud-java by GoogleCloudPlatform.

the class StorageImpl method create.

private Blob create(BlobInfo info, final InputStream content, BlobTargetOption... options) {
    final StorageObject blobPb = info.toPb();
    final Map<StorageRpc.Option, ?> optionsMap = optionMap(info, options);
    try {
        return Blob.fromPb(this, runWithRetries(new Callable<StorageObject>() {

            @Override
            public StorageObject call() {
                return storageRpc.create(blobPb, firstNonNull(content, new ByteArrayInputStream(EMPTY_BYTE_ARRAY)), optionsMap);
            }
        }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()));
    } catch (RetryHelperException e) {
        throw StorageException.translateAndThrow(e);
    }
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) ByteArrayInputStream(java.io.ByteArrayInputStream) Callable(java.util.concurrent.Callable) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException)

Example 3 with RetryHelperException

use of com.google.cloud.RetryHelper.RetryHelperException in project google-cloud-java by GoogleCloudPlatform.

the class StorageImpl method delete.

@Override
public boolean delete(BlobId blob, BlobSourceOption... options) {
    final StorageObject storageObject = blob.toPb();
    final Map<StorageRpc.Option, ?> optionsMap = optionMap(blob, options);
    try {
        return runWithRetries(new Callable<Boolean>() {

            @Override
            public Boolean call() {
                return storageRpc.delete(storageObject, optionsMap);
            }
        }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock());
    } catch (RetryHelperException e) {
        throw StorageException.translateAndThrow(e);
    }
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException)

Example 4 with RetryHelperException

use of com.google.cloud.RetryHelper.RetryHelperException in project google-cloud-java by GoogleCloudPlatform.

the class StorageImpl method readAllBytes.

@Override
public byte[] readAllBytes(BlobId blob, BlobSourceOption... options) {
    final StorageObject storageObject = blob.toPb();
    final Map<StorageRpc.Option, ?> optionsMap = optionMap(blob, options);
    try {
        return runWithRetries(new Callable<byte[]>() {

            @Override
            public byte[] call() {
                return storageRpc.load(storageObject, optionsMap);
            }
        }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock());
    } catch (RetryHelperException e) {
        throw StorageException.translateAndThrow(e);
    }
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException)

Example 5 with RetryHelperException

use of com.google.cloud.RetryHelper.RetryHelperException in project google-cloud-java by GoogleCloudPlatform.

the class ComputeExceptionTest method testTranslateAndThrow.

@Test
public void testTranslateAndThrow() throws Exception {
    Exception cause = new ComputeException(500, "message");
    RetryHelperException exceptionMock = createMock(RetryHelperException.class);
    expect(exceptionMock.getCause()).andReturn(cause).times(2);
    replay(exceptionMock);
    try {
        ComputeException.translateAndThrow(exceptionMock);
    } catch (BaseServiceException ex) {
        assertEquals(500, ex.getCode());
        assertEquals("message", ex.getMessage());
        assertTrue(ex.isRetryable());
    } finally {
        verify(exceptionMock);
    }
    cause = new IllegalArgumentException("message");
    exceptionMock = createMock(RetryHelperException.class);
    expect(exceptionMock.getMessage()).andReturn("message").times(1);
    expect(exceptionMock.getCause()).andReturn(cause).times(2);
    replay(exceptionMock);
    try {
        ComputeException.translateAndThrow(exceptionMock);
    } catch (BaseServiceException ex) {
        assertEquals(ComputeException.UNKNOWN_CODE, ex.getCode());
        assertEquals("message", ex.getMessage());
        assertFalse(ex.isRetryable());
        assertSame(cause, ex.getCause());
    } finally {
        verify(exceptionMock);
    }
}
Also used : BaseServiceException(com.google.cloud.BaseServiceException) BaseServiceException(com.google.cloud.BaseServiceException) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException) IOException(java.io.IOException) SocketTimeoutException(java.net.SocketTimeoutException) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException) Test(org.junit.Test)

Aggregations

RetryHelperException (com.google.cloud.RetryHelper.RetryHelperException)14 StorageObject (com.google.api.services.storage.model.StorageObject)7 BaseServiceException (com.google.cloud.BaseServiceException)6 IOException (java.io.IOException)6 SocketTimeoutException (java.net.SocketTimeoutException)6 Test (org.junit.Test)6 Callable (java.util.concurrent.Callable)3 HttpResponseException (com.google.api.client.http.HttpResponseException)2 TestIamPermissionsResponse (com.google.api.services.storage.model.TestIamPermissionsResponse)1 RewriteResponse (com.google.cloud.storage.spi.v1.StorageRpc.RewriteResponse)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1