Skip to content

Commit 4dc40fb

Browse files
authored
---
yaml --- r: 20773 b: refs/heads/autosynth-iamcredentials c: 692a530 h: refs/heads/master i: 20771: e999216
1 parent fcfc335 commit 4dc40fb

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ refs/heads/pubsub-ordering-keys: 858d4e986a0ba48e08f00d42f51cbdecb175f5d6
162162
refs/tags/v0.75.0: c3673089ae09a897c1b4cf7dfe167fe4f8ab32fb
163163
refs/tags/v0.76.0: 395b016826d3ddf9cb8b34919636df15a4dbd032
164164
refs/tags/v0.77.0: 28a85a77883ccf5d48f297fd0ef3b3dca6ce01f0
165-
refs/heads/autosynth-iamcredentials: e0d4cdd7e6b914551b9afdabc240a01044bad7b0
165+
refs/heads/autosynth-iamcredentials: 692a530970f114fd8b2a6976d79af41d4fc8e4dd
166166
refs/heads/release-google-cloud-java-v0.78.0: fae5e980779cf0173a152636b278015b9f60ee55
167167
refs/heads/release-google-cloud-java-v0.81.0: 0352cd0dd11f4fd1fbd1ff16e7a96beaccc7b475
168168
refs/heads/release-google-cloud-java-v0.81.1-SNAPSHOT: 5a74ccb1f12506a3b67b65521881298fde20bd6f

branches/autosynth-iamcredentials/google-cloud-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerExceptionFactory.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import static com.google.cloud.spanner.SpannerException.DoNotConstructDirectly;
2020

21+
import com.google.api.gax.grpc.GrpcStatusCode;
22+
import com.google.api.gax.rpc.ApiException;
2123
import com.google.common.base.MoreObjects;
2224
import com.google.common.base.Predicate;
2325
import io.grpc.Context;
@@ -48,6 +50,28 @@ public static SpannerException propagateInterrupt(InterruptedException e) {
4850
return SpannerExceptionFactory.newSpannerException(ErrorCode.CANCELLED, "Interrupted", e);
4951
}
5052

53+
/**
54+
* Transforms a {@code TimeoutException} to a {@code SpannerException}.
55+
*
56+
* <pre>
57+
* <code>
58+
* try {
59+
* Spanner spanner = SpannerOptions.getDefaultInstance();
60+
* spanner
61+
* .getDatabaseAdminClient()
62+
* .createDatabase("[INSTANCE_ID]", "[DATABASE_ID]", [STATEMENTS])
63+
* .get();
64+
* } catch (TimeoutException e) {
65+
* propagateTimeout(e);
66+
* }
67+
* </code>
68+
* </pre>
69+
*/
70+
public static SpannerException propagateTimeout(TimeoutException e) {
71+
return SpannerExceptionFactory.newSpannerException(
72+
ErrorCode.DEADLINE_EXCEEDED, "Operation did not complete in the given time", e);
73+
}
74+
5175
/**
5276
* Creates a new exception based on {@code cause}.
5377
*
@@ -71,6 +95,8 @@ public static SpannerException newSpannerException(@Nullable Context context, Th
7195
return newSpannerExceptionPreformatted(e.getErrorCode(), e.getMessage(), e);
7296
} else if (cause instanceof CancellationException) {
7397
return newSpannerExceptionForCancellation(context, cause);
98+
} else if (cause instanceof ApiException) {
99+
return fromApiException((ApiException) cause);
74100
}
75101
// Extract gRPC status. This will produce "UNKNOWN" for non-gRPC exceptions.
76102
Status status = Status.fromThrowable(cause);
@@ -120,6 +146,17 @@ private static SpannerException newSpannerExceptionPreformatted(
120146
}
121147
}
122148

149+
private static SpannerException fromApiException(ApiException exception) {
150+
Status.Code code = ((GrpcStatusCode) exception.getStatusCode()).getTransportCode();
151+
ErrorCode errorCode = ErrorCode.fromGrpcStatus(Status.fromCode(code));
152+
if (exception.getCause() != null) {
153+
return SpannerExceptionFactory.newSpannerException(
154+
errorCode, exception.getMessage(), exception.getCause());
155+
} else {
156+
return SpannerExceptionFactory.newSpannerException(errorCode, exception.getMessage());
157+
}
158+
}
159+
123160
private static boolean isRetryable(ErrorCode code, @Nullable Throwable cause) {
124161
switch (code) {
125162
case INTERNAL:

0 commit comments

Comments
 (0)