diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpc.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpc.java index dffcc3f0e16f..3e875ff2b8ba 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpc.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpc.java @@ -79,12 +79,12 @@ public int httpStatus() { private final int httpStatus; private final boolean retryable; - public DatastoreRpcException(Reason reason) { - this(reason.name(), reason.httpStatus, reason.retryable, reason.description); + public DatastoreRpcException(Reason reason, Throwable cause) { + this(reason.name(), reason.httpStatus, reason.retryable, reason.description, cause); } - public DatastoreRpcException(String reason, int httpStatus, boolean retryable, String message) { - super(message); + public DatastoreRpcException(String reason, int httpStatus, boolean retryable, String message, Throwable cause) { + super(message, cause); this.reason = reason; this.httpStatus = httpStatus; this.retryable = retryable; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java index 4771895eec99..fa993c508a0b 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java @@ -124,7 +124,7 @@ private static DatastoreRpcException translate(DatastoreException exception) { reason = HTTP_STATUS_TO_REASON.get(exception.getCode()); } if (reason != null) { - return new DatastoreRpcException(reason); + return new DatastoreRpcException(reason, exception); } else { boolean retryable = false; reasonStr = "Unknown"; @@ -132,7 +132,7 @@ private static DatastoreRpcException translate(DatastoreException exception) { retryable = true; reasonStr = "Request timeout"; } - return new DatastoreRpcException(reasonStr, exception.getCode(), retryable, message); + return new DatastoreRpcException(reasonStr, exception.getCode(), retryable, message, exception); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java index 9ad836b15a4e..9f28dccd4930 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java @@ -42,12 +42,15 @@ public void testDatastoreError() throws Exception { @Test public void testTranslateAndThrow() throws Exception { + DatastoreRpcException toTranslate = null; // this should be preserved as a cause for (Reason reason : Reason.values()) { try { - DatastoreException.translateAndThrow(new DatastoreRpcException(reason)); + toTranslate = new DatastoreRpcException(reason, null); + DatastoreException.translateAndThrow(toTranslate); fail("Exception expected"); } catch (DatastoreException ex) { assertEquals(reason.name(), ex.datastoreError().name()); + assertEquals(toTranslate, ex.getCause()); } } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index 029b161c1c38..9874054f5744 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -734,7 +734,7 @@ public void testRetryableException() throws Exception { EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) .andReturn(rpcMock); EasyMock.expect(rpcMock.lookup(requestPb)) - .andThrow(new DatastoreRpc.DatastoreRpcException(Reason.UNAVAILABLE)) + .andThrow(new DatastoreRpc.DatastoreRpcException(Reason.UNAVAILABLE, null)) .andReturn(responsePb); EasyMock.replay(rpcFactoryMock, rpcMock); DatastoreOptions options = this.options.toBuilder() @@ -756,7 +756,7 @@ public void testNonRetryableException() throws Exception { EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) .andReturn(rpcMock); EasyMock.expect(rpcMock.lookup(requestPb)) - .andThrow(new DatastoreRpc.DatastoreRpcException(Reason.PERMISSION_DENIED)) + .andThrow(new DatastoreRpc.DatastoreRpcException(Reason.PERMISSION_DENIED, null)) .times(1); EasyMock.replay(rpcFactoryMock, rpcMock); RetryParams retryParams = RetryParams.builder().retryMinAttempts(2).build();