Skip to content

Commit d82500d

Browse files
committed
Merge pull request #202 from mziccard/fix-and-test-storage-exceptions
Make StorageImpl and related methods throw StorageException
2 parents f3a19a6 + 36f2867 commit d82500d

File tree

5 files changed

+268
-131
lines changed

5 files changed

+268
-131
lines changed

gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobReadChannelImpl.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.gcloud.RetryHelper.runWithRetries;
2020

2121
import com.google.api.services.storage.model.StorageObject;
22+
import com.google.gcloud.RetryHelper;
2223
import com.google.gcloud.spi.StorageRpc;
2324

2425
import java.io.IOException;
@@ -119,12 +120,16 @@ public int read(ByteBuffer byteBuffer) throws IOException {
119120
return -1;
120121
}
121122
final int toRead = Math.max(byteBuffer.remaining(), chunkSize);
122-
buffer = runWithRetries(new Callable<byte[]>() {
123-
@Override
124-
public byte[] call() {
125-
return storageRpc.read(storageObject, requestOptions, position, toRead);
126-
}
127-
}, serviceOptions.retryParams(), StorageImpl.EXCEPTION_HANDLER);
123+
try {
124+
buffer = runWithRetries(new Callable<byte[]>() {
125+
@Override
126+
public byte[] call() {
127+
return storageRpc.read(storageObject, requestOptions, position, toRead);
128+
}
129+
}, serviceOptions.retryParams(), StorageImpl.EXCEPTION_HANDLER);
130+
} catch (RetryHelper.RetryHelperException e) {
131+
throw StorageException.translateAndThrow(e);
132+
}
128133
if (toRead > buffer.length) {
129134
endOfStream = true;
130135
if (buffer.length == 0) {

gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriteChannelImpl.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static java.util.concurrent.Executors.callable;
2121

2222
import com.google.api.services.storage.model.StorageObject;
23+
import com.google.gcloud.RetryHelper;
2324
import com.google.gcloud.spi.StorageRpc;
2425

2526
import java.io.IOException;
@@ -68,12 +69,16 @@ private void writeObject(ObjectOutputStream out) throws IOException {
6869
private void flush(boolean compact) {
6970
if (limit >= chunkSize || compact && limit >= MIN_CHUNK_SIZE) {
7071
final int length = limit - limit % MIN_CHUNK_SIZE;
71-
runWithRetries(callable(new Runnable() {
72-
@Override
73-
public void run() {
74-
storageRpc.write(uploadId, buffer, 0, storageObject, position, length, false);
75-
}
76-
}), options.retryParams(), StorageImpl.EXCEPTION_HANDLER);
72+
try {
73+
runWithRetries(callable(new Runnable() {
74+
@Override
75+
public void run() {
76+
storageRpc.write(uploadId, buffer, 0, storageObject, position, length, false);
77+
}
78+
}), options.retryParams(), StorageImpl.EXCEPTION_HANDLER);
79+
} catch (RetryHelper.RetryHelperException e) {
80+
throw StorageException.translateAndThrow(e);
81+
}
7782
position += length;
7883
limit -= length;
7984
byte[] temp = new byte[compact ? limit : chunkSize];
@@ -124,12 +129,16 @@ public boolean isOpen() {
124129
@Override
125130
public void close() throws IOException {
126131
if (isOpen) {
127-
runWithRetries(callable(new Runnable() {
128-
@Override
129-
public void run() {
130-
storageRpc.write(uploadId, buffer, 0, storageObject, position, limit, true);
131-
}
132-
}), options.retryParams(), StorageImpl.EXCEPTION_HANDLER);
132+
try {
133+
runWithRetries(callable(new Runnable() {
134+
@Override
135+
public void run() {
136+
storageRpc.write(uploadId, buffer, 0, storageObject, position, limit, true);
137+
}
138+
}), options.retryParams(), StorageImpl.EXCEPTION_HANDLER);
139+
} catch (RetryHelper.RetryHelperException e) {
140+
throw StorageException.translateAndThrow(e);
141+
}
133142
position += buffer.length;
134143
isOpen = false;
135144
buffer = null;

gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageException.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.google.gcloud.storage;
1818

19+
import com.google.gcloud.RetryHelper;
20+
import com.google.gcloud.RetryHelper.RetryHelperException;
21+
1922
/**
2023
* Storage service exception.
2124
*
@@ -25,6 +28,7 @@
2528
public class StorageException extends RuntimeException {
2629

2730
private static final long serialVersionUID = -3748432005065428084L;
31+
private static final int UNKNOWN_CODE = -1;
2832

2933
private final int code;
3034
private final boolean retryable;
@@ -45,4 +49,21 @@ public int code() {
4549
public boolean retryable() {
4650
return retryable;
4751
}
52+
53+
/**
54+
* Translate RetryHelperException to the StorageException that caused the error. This method will
55+
* always throw an exception.
56+
*
57+
* @throws StorageException when {@code ex} was caused by a {@code StorageException}
58+
* @throws RetryInterruptedException when {@code ex} is a {@code RetryInterruptedException}
59+
*/
60+
static StorageException translateAndThrow(RetryHelperException ex) {
61+
if (ex.getCause() instanceof StorageException) {
62+
throw (StorageException) ex.getCause();
63+
}
64+
if (ex instanceof RetryHelper.RetryInterruptedException) {
65+
RetryHelper.RetryInterruptedException.propagate();
66+
}
67+
throw new StorageException(UNKNOWN_CODE, ex.getMessage(), false);
68+
}
4869
}

0 commit comments

Comments
 (0)