Skip to content

Commit

Permalink
info: add additional information to error message (#50)
Browse files Browse the repository at this point in the history
Any error that might occur during the listing of databases of an
instance will now also include the name of the instance and the
page token that was used to try to get the databases. This makes
it easier to debug why a particular RPC might have failed.

Closes #17
  • Loading branch information
olavloite committed Jan 30, 2020
1 parent 933effa commit 38ae1e3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Expand Up @@ -28,6 +28,7 @@
import com.google.cloud.spanner.SpannerImpl.PageFetcher;
import com.google.cloud.spanner.spi.v1.SpannerRpc;
import com.google.cloud.spanner.spi.v1.SpannerRpc.Paginated;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.protobuf.Empty;
import com.google.spanner.admin.database.v1.CreateDatabaseMetadata;
Expand Down Expand Up @@ -155,7 +156,18 @@ public Page<Database> listDatabases(String instanceId, ListOption... options) {
@Override
public Paginated<com.google.spanner.admin.database.v1.Database> getNextPage(
String nextPageToken) {
return rpc.listDatabases(instanceName, pageSize, nextPageToken);
try {
return rpc.listDatabases(instanceName, pageSize, nextPageToken);
} catch (SpannerException e) {
throw SpannerExceptionFactory.newSpannerException(
e.getErrorCode(),
String.format(
"Failed to list the databases of %s with pageToken %s: %s",
instanceName,
MoreObjects.firstNonNull(nextPageToken, "<null>"),
e.getMessage()),
e);
}
}

@Override
Expand Down
Expand Up @@ -43,6 +43,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -170,6 +171,39 @@ public void listDatabases() {
assertThat(dbs.size()).isEqualTo(2);
}

@Test
public void listDatabasesError() {
when(rpc.listDatabases(INSTANCE_NAME, 1, null))
.thenThrow(
SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Test error"));
try {
client.listDatabases(INSTANCE_ID, Options.pageSize(1));
Assert.fail("Missing expected exception");
} catch (SpannerException e) {
assertThat(e.getMessage()).contains(INSTANCE_NAME);
// Assert that the call was done without a page token.
assertThat(e.getMessage()).contains("with pageToken <null>");
}
}

@Test
public void listDatabaseErrorWithToken() {
String pageToken = "token";
when(rpc.listDatabases(INSTANCE_NAME, 1, null))
.thenReturn(new Paginated<>(ImmutableList.<Database>of(getDatabaseProto()), pageToken));
when(rpc.listDatabases(INSTANCE_NAME, 1, pageToken))
.thenThrow(
SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Test error"));
try {
Lists.newArrayList(client.listDatabases(INSTANCE_ID, Options.pageSize(1)).iterateAll());
Assert.fail("Missing expected exception");
} catch (SpannerException e) {
assertThat(e.getMessage()).contains(INSTANCE_NAME);
// Assert that the call was done without a page token.
assertThat(e.getMessage()).contains(String.format("with pageToken %s", pageToken));
}
}

@Test
public void getDatabaseIAMPolicy() {
when(rpc.getDatabaseAdminIAMPolicy(DB_NAME))
Expand Down

0 comments on commit 38ae1e3

Please sign in to comment.