Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b62017c

Browse files
committed
Merge pull request googleapis#546 from mderka/exception-cause
Fixed googleapis#478 - Make DatastoreException informative.
2 parents 5a23e57 + 1583312 commit b62017c

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ public int httpStatus() {
7979
private final int httpStatus;
8080
private final boolean retryable;
8181

82-
public DatastoreRpcException(Reason reason) {
83-
this(reason.name(), reason.httpStatus, reason.retryable, reason.description);
82+
public DatastoreRpcException(Reason reason, Throwable cause) {
83+
this(reason.name(), reason.httpStatus, reason.retryable, reason.description, cause);
8484
}
8585

86-
public DatastoreRpcException(String reason, int httpStatus, boolean retryable, String message) {
87-
super(message);
86+
public DatastoreRpcException(String reason, int httpStatus, boolean retryable, String message, Throwable cause) {
87+
super(message, cause);
8888
this.reason = reason;
8989
this.httpStatus = httpStatus;
9090
this.retryable = retryable;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,15 @@ private static DatastoreRpcException translate(DatastoreException exception) {
124124
reason = HTTP_STATUS_TO_REASON.get(exception.getCode());
125125
}
126126
if (reason != null) {
127-
return new DatastoreRpcException(reason);
127+
return new DatastoreRpcException(reason, exception);
128128
} else {
129129
boolean retryable = false;
130130
reasonStr = "Unknown";
131131
if (exception.getCause() instanceof SocketTimeoutException) {
132132
retryable = true;
133133
reasonStr = "Request timeout";
134134
}
135-
return new DatastoreRpcException(reasonStr, exception.getCode(), retryable, message);
135+
return new DatastoreRpcException(reasonStr, exception.getCode(), retryable, message, exception);
136136
}
137137
}
138138

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@ public void testDatastoreError() throws Exception {
4242

4343
@Test
4444
public void testTranslateAndThrow() throws Exception {
45+
DatastoreRpcException toTranslate = null; // should be preserved as a cause
4546
for (Reason reason : Reason.values()) {
4647
try {
47-
DatastoreException.translateAndThrow(new DatastoreRpcException(reason));
48+
toTranslate = new DatastoreRpcException(reason, null);
49+
DatastoreException.translateAndThrow(toTranslate);
4850
fail("Exception expected");
4951
} catch (DatastoreException ex) {
5052
assertEquals(reason.name(), ex.datastoreError().name());
53+
assertEquals(toTranslate, ex.getCause());
5154
}
5255
}
5356
}

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ public void testRetryableException() throws Exception {
734734
EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class)))
735735
.andReturn(rpcMock);
736736
EasyMock.expect(rpcMock.lookup(requestPb))
737-
.andThrow(new DatastoreRpc.DatastoreRpcException(Reason.UNAVAILABLE))
737+
.andThrow(new DatastoreRpc.DatastoreRpcException(Reason.UNAVAILABLE, null))
738738
.andReturn(responsePb);
739739
EasyMock.replay(rpcFactoryMock, rpcMock);
740740
DatastoreOptions options = this.options.toBuilder()
@@ -756,7 +756,7 @@ public void testNonRetryableException() throws Exception {
756756
EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class)))
757757
.andReturn(rpcMock);
758758
EasyMock.expect(rpcMock.lookup(requestPb))
759-
.andThrow(new DatastoreRpc.DatastoreRpcException(Reason.PERMISSION_DENIED))
759+
.andThrow(new DatastoreRpc.DatastoreRpcException(Reason.PERMISSION_DENIED, null))
760760
.times(1);
761761
EasyMock.replay(rpcFactoryMock, rpcMock);
762762
RetryParams retryParams = RetryParams.builder().retryMinAttempts(2).build();

0 commit comments

Comments
 (0)