Skip to content

Commit

Permalink
Fixed googleapis#478 - Make DatastoreException informative.
Browse files Browse the repository at this point in the history
Makes DatastoreException message more informative by preserving a cause.
Fixes googleapis#478
  • Loading branch information
mderka committed Jan 11, 2016
1 parent 5a23e57 commit 25ad65f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ 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";
if (exception.getCause() instanceof SocketTimeoutException) {
retryable = true;
reasonStr = "Request timeout";
}
return new DatastoreRpcException(reasonStr, exception.getCode(), retryable, message);
return new DatastoreRpcException(reasonStr, exception.getCode(), retryable, message, exception);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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();
Expand Down

0 comments on commit 25ad65f

Please sign in to comment.