diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreException.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreException.java index 42c0079e0a77..a70ba9963f22 100644 --- a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreException.java +++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreException.java @@ -34,17 +34,24 @@ public final class DatastoreException extends BaseServiceException { // see https://cloud.google.com/datastore/docs/concepts/errors#Error_Codes" private static final Set RETRYABLE_ERRORS = ImmutableSet.of( - new Error(10, "ABORTED"), new Error(4, "DEADLINE_EXCEEDED"), new Error(14, "UNAVAILABLE")); + new Error(10, "ABORTED", true), + new Error(4, "DEADLINE_EXCEEDED", false), + new Error(14, "UNAVAILABLE", true)); private static final long serialVersionUID = 2663750991205874435L; public DatastoreException(int code, String message, String reason) { - this(code, message, reason, null); + this(code, message, reason, true, null); } public DatastoreException(int code, String message, String reason, Throwable cause) { super(code, message, reason, true, cause); } + public DatastoreException(int code, String message, String reason, boolean idempotent, + Throwable cause) { + super(code, message, reason, idempotent, cause); + } + public DatastoreException(IOException exception) { super(exception, true); } diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/spi/DefaultDatastoreRpc.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/spi/DefaultDatastoreRpc.java index cbb2436c82e6..54b9456e227a 100644 --- a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/spi/DefaultDatastoreRpc.java +++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/spi/DefaultDatastoreRpc.java @@ -93,6 +93,11 @@ private static String removeScheme(String url) { private static DatastoreException translate( com.google.datastore.v1.client.DatastoreException exception) { + return translate(exception, true); + } + + private static DatastoreException translate( + com.google.datastore.v1.client.DatastoreException exception, boolean idempotent) { String reason = ""; if (exception.getCode() != null) { reason = exception.getCode().name(); @@ -103,7 +108,7 @@ private static DatastoreException translate( } } return new DatastoreException( - exception.getCode().getNumber(), exception.getMessage(), reason, exception); + exception.getCode().getNumber(), exception.getMessage(), reason, idempotent, exception); } @Override @@ -111,7 +116,6 @@ public AllocateIdsResponse allocateIds(AllocateIdsRequest request) { try { return client.allocateIds(request); } catch (com.google.datastore.v1.client.DatastoreException ex) { - throw translate(ex); } } @@ -130,7 +134,7 @@ public CommitResponse commit(CommitRequest request) { try { return client.commit(request); } catch (com.google.datastore.v1.client.DatastoreException ex) { - throw translate(ex); + throw translate(ex, request.getMode() == CommitRequest.Mode.NON_TRANSACTIONAL); } }