Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #478 - Make DatastoreException informative. #546

Merged
merged 1 commit into from
Jan 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; // 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