Skip to content

Commit 0a05851

Browse files
committed
Translate SocketTimeoutException to retryable service exception
1 parent a23d8f9 commit 0a05851

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.json.JSONTokener;
4343

4444
import java.net.InetAddress;
45+
import java.net.SocketTimeoutException;
4546
import java.net.URL;
4647
import java.util.HashMap;
4748
import java.util.Map;
@@ -121,9 +122,15 @@ private static DatastoreRpcException translate(DatastoreException exception) {
121122
if (reason == null) {
122123
reason = HTTP_STATUS_TO_REASON.get(exception.getCode());
123124
}
124-
return reason != null
125-
? new DatastoreRpcException(reason)
126-
: new DatastoreRpcException("Unknown", exception.getCode(), false, message);
125+
if (reason != null) {
126+
return new DatastoreRpcException(reason);
127+
} else {
128+
boolean retryable = false;
129+
if (exception.getCause() instanceof SocketTimeoutException) {
130+
retryable = true;
131+
}
132+
return new DatastoreRpcException("Unknown", exception.getCode(), retryable, message);
133+
}
127134
}
128135

129136
@Override

gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import java.io.ByteArrayOutputStream;
6969
import java.io.IOException;
7070
import java.io.InputStream;
71+
import java.net.SocketTimeoutException;
7172
import java.util.ArrayList;
7273
import java.util.Iterator;
7374
import java.util.List;
@@ -101,7 +102,11 @@ private static StorageException translate(IOException exception) {
101102
&& ((GoogleJsonResponseException) exception).getDetails() != null) {
102103
translated = translate(((GoogleJsonResponseException) exception).getDetails());
103104
} else {
104-
translated = new StorageException(0, exception.getMessage(), false);
105+
boolean retryable = false;
106+
if (exception instanceof SocketTimeoutException) {
107+
retryable = true;
108+
}
109+
translated = new StorageException(0, exception.getMessage(), retryable);
105110
}
106111
translated.initCause(exception);
107112
return translated;

0 commit comments

Comments
 (0)