|
19 | 19 | import com.google.api.core.ApiFunction; |
20 | 20 | import com.google.api.core.ApiFuture; |
21 | 21 | import com.google.api.core.ApiFutures; |
| 22 | +import com.google.api.gax.rpc.ApiExceptions; |
22 | 23 | import com.google.api.resourcenames.ResourceName; |
23 | 24 | import com.google.bigtable.admin.v2.AppProfileName; |
24 | 25 | import com.google.bigtable.admin.v2.ClusterName; |
|
46 | 47 | import com.google.common.base.Verify; |
47 | 48 | import com.google.common.collect.ImmutableList; |
48 | 49 | import com.google.common.collect.Lists; |
49 | | -import com.google.common.util.concurrent.Futures; |
50 | 50 | import com.google.common.util.concurrent.MoreExecutors; |
51 | | -import com.google.common.util.concurrent.UncheckedExecutionException; |
52 | 51 | import com.google.iam.v1.GetIamPolicyRequest; |
53 | 52 | import com.google.iam.v1.SetIamPolicyRequest; |
54 | 53 | import com.google.iam.v1.TestIamPermissionsRequest; |
@@ -160,7 +159,7 @@ public void close() { |
160 | 159 | */ |
161 | 160 | @SuppressWarnings("WeakerAccess") |
162 | 161 | public Instance createInstance(CreateInstanceRequest request) { |
163 | | - return awaitFuture(createInstanceAsync(request)); |
| 162 | + return ApiExceptions.callAndTranslateApiException(createInstanceAsync(request)); |
164 | 163 | } |
165 | 164 |
|
166 | 165 | /** |
@@ -208,7 +207,7 @@ public Instance apply(com.google.bigtable.admin.v2.Instance proto) { |
208 | 207 | */ |
209 | 208 | @SuppressWarnings("WeakerAccess") |
210 | 209 | public Instance updateInstance(UpdateInstanceRequest request) { |
211 | | - return awaitFuture(updateInstanceAsync(request)); |
| 210 | + return ApiExceptions.callAndTranslateApiException(updateInstanceAsync(request)); |
212 | 211 | } |
213 | 212 |
|
214 | 213 | /** |
@@ -251,7 +250,7 @@ public Instance apply(com.google.bigtable.admin.v2.Instance proto) { |
251 | 250 | */ |
252 | 251 | @SuppressWarnings("WeakerAccess") |
253 | 252 | public Instance getInstance(String id) { |
254 | | - return awaitFuture(getInstanceAsync(id)); |
| 253 | + return ApiExceptions.callAndTranslateApiException(getInstanceAsync(id)); |
255 | 254 | } |
256 | 255 |
|
257 | 256 | /** |
@@ -302,7 +301,7 @@ public Instance apply(com.google.bigtable.admin.v2.Instance proto) { |
302 | 301 | */ |
303 | 302 | @SuppressWarnings("WeakerAccess") |
304 | 303 | public List<Instance> listInstances() { |
305 | | - return awaitFuture(listInstancesAsync()); |
| 304 | + return ApiExceptions.callAndTranslateApiException(listInstancesAsync()); |
306 | 305 | } |
307 | 306 |
|
308 | 307 | /** |
@@ -385,7 +384,7 @@ public List<Instance> apply(com.google.bigtable.admin.v2.ListInstancesResponse p |
385 | 384 | */ |
386 | 385 | @SuppressWarnings("WeakerAccess") |
387 | 386 | public void deleteInstance(String instanceId) { |
388 | | - awaitFuture(deleteInstanceAsync(instanceId)); |
| 387 | + ApiExceptions.callAndTranslateApiException(deleteInstanceAsync(instanceId)); |
389 | 388 | } |
390 | 389 |
|
391 | 390 | /** |
@@ -434,7 +433,7 @@ public Void apply(Empty input) { |
434 | 433 | */ |
435 | 434 | @SuppressWarnings("WeakerAccess") |
436 | 435 | public Cluster createCluster(CreateClusterRequest request) { |
437 | | - return awaitFuture(createClusterAsync(request)); |
| 436 | + return ApiExceptions.callAndTranslateApiException(createClusterAsync(request)); |
438 | 437 | } |
439 | 438 |
|
440 | 439 | /** |
@@ -478,7 +477,7 @@ public Cluster apply(com.google.bigtable.admin.v2.Cluster proto) { |
478 | 477 | */ |
479 | 478 | @SuppressWarnings("WeakerAccess") |
480 | 479 | public Cluster getCluster(String instanceId, String clusterId) { |
481 | | - return awaitFuture(getClusterAsync(instanceId, clusterId)); |
| 480 | + return ApiExceptions.callAndTranslateApiException(getClusterAsync(instanceId, clusterId)); |
482 | 481 | } |
483 | 482 |
|
484 | 483 | /** |
@@ -531,7 +530,7 @@ public Cluster apply(com.google.bigtable.admin.v2.Cluster proto) { |
531 | 530 | */ |
532 | 531 | @SuppressWarnings("WeakerAccess") |
533 | 532 | public List<Cluster> listClusters(String instanceId) { |
534 | | - return awaitFuture(listClustersAsync(instanceId)); |
| 533 | + return ApiExceptions.callAndTranslateApiException(listClustersAsync(instanceId)); |
535 | 534 | } |
536 | 535 |
|
537 | 536 | /** |
@@ -615,7 +614,8 @@ public List<Cluster> apply(com.google.bigtable.admin.v2.ListClustersResponse pro |
615 | 614 | */ |
616 | 615 | @SuppressWarnings("WeakerAccess") |
617 | 616 | public Cluster resizeCluster(String instanceId, String clusterId, int numServeNodes) { |
618 | | - return awaitFuture(resizeClusterAsync(instanceId, clusterId, numServeNodes)); |
| 617 | + return ApiExceptions.callAndTranslateApiException( |
| 618 | + resizeClusterAsync(instanceId, clusterId, numServeNodes)); |
619 | 619 | } |
620 | 620 |
|
621 | 621 | /** |
@@ -662,7 +662,7 @@ public Cluster apply(com.google.bigtable.admin.v2.Cluster proto) { |
662 | 662 | */ |
663 | 663 | @SuppressWarnings("WeakerAccess") |
664 | 664 | public void deleteCluster(String instanceId, String clusterId) { |
665 | | - awaitFuture(deleteClusterAsync(instanceId, clusterId)); |
| 665 | + ApiExceptions.callAndTranslateApiException(deleteClusterAsync(instanceId, clusterId)); |
666 | 666 | } |
667 | 667 |
|
668 | 668 | /** |
@@ -713,7 +713,7 @@ public Void apply(Empty input) { |
713 | 713 | */ |
714 | 714 | @SuppressWarnings("WeakerAccess") |
715 | 715 | public AppProfile createAppProfile(CreateAppProfileRequest request) { |
716 | | - return awaitFuture(createAppProfileAsync(request)); |
| 716 | + return ApiExceptions.callAndTranslateApiException(createAppProfileAsync(request)); |
717 | 717 | } |
718 | 718 |
|
719 | 719 | /** |
@@ -759,7 +759,7 @@ public AppProfile apply(com.google.bigtable.admin.v2.AppProfile proto) { |
759 | 759 | */ |
760 | 760 | @SuppressWarnings("WeakerAccess") |
761 | 761 | public AppProfile getAppProfile(String instanceId, String appProfileId) { |
762 | | - return awaitFuture(getAppProfileAsync(instanceId, appProfileId)); |
| 762 | + return ApiExceptions.callAndTranslateApiException(getAppProfileAsync(instanceId, appProfileId)); |
763 | 763 | } |
764 | 764 |
|
765 | 765 | /** |
@@ -808,7 +808,7 @@ public AppProfile apply(com.google.bigtable.admin.v2.AppProfile proto) { |
808 | 808 | */ |
809 | 809 | @SuppressWarnings("WeakerAccess") |
810 | 810 | public List<AppProfile> listAppProfiles(String instanceId) { |
811 | | - return awaitFuture(listAppProfilesAsync(instanceId)); |
| 811 | + return ApiExceptions.callAndTranslateApiException(listAppProfilesAsync(instanceId)); |
812 | 812 | } |
813 | 813 |
|
814 | 814 | /** |
@@ -911,7 +911,7 @@ public List<AppProfile> apply(List<com.google.bigtable.admin.v2.AppProfile> inpu |
911 | 911 | */ |
912 | 912 | @SuppressWarnings("WeakerAccess") |
913 | 913 | public AppProfile updateAppProfile(UpdateAppProfileRequest request) { |
914 | | - return awaitFuture(updateAppProfileAsync(request)); |
| 914 | + return ApiExceptions.callAndTranslateApiException(updateAppProfileAsync(request)); |
915 | 915 | } |
916 | 916 |
|
917 | 917 | /** |
@@ -966,7 +966,7 @@ public AppProfile apply(com.google.bigtable.admin.v2.AppProfile proto) { |
966 | 966 | */ |
967 | 967 | @SuppressWarnings("WeakerAccess") |
968 | 968 | public void deleteAppProfile(String instanceId, String appProfileId) { |
969 | | - awaitFuture(deleteAppProfileAsync(instanceId, appProfileId)); |
| 969 | + ApiExceptions.callAndTranslateApiException(deleteAppProfileAsync(instanceId, appProfileId)); |
970 | 970 | } |
971 | 971 |
|
972 | 972 | /** |
@@ -1016,7 +1016,7 @@ public Void apply(Empty input) { |
1016 | 1016 | */ |
1017 | 1017 | @SuppressWarnings("WeakerAccess") |
1018 | 1018 | public Policy getIamPolicy(String instanceId) { |
1019 | | - return awaitFuture(getIamPolicyAsync(instanceId)); |
| 1019 | + return ApiExceptions.callAndTranslateApiException(getIamPolicyAsync(instanceId)); |
1020 | 1020 | } |
1021 | 1021 |
|
1022 | 1022 | /** |
@@ -1083,7 +1083,7 @@ public Policy apply(com.google.iam.v1.Policy proto) { |
1083 | 1083 | */ |
1084 | 1084 | @SuppressWarnings("WeakerAccess") |
1085 | 1085 | public Policy setIamPolicy(String instanceId, Policy policy) { |
1086 | | - return awaitFuture(setIamPolicyAsync(instanceId, policy)); |
| 1086 | + return ApiExceptions.callAndTranslateApiException(setIamPolicyAsync(instanceId, policy)); |
1087 | 1087 | } |
1088 | 1088 |
|
1089 | 1089 | /** |
@@ -1210,7 +1210,8 @@ public ApiFuture<List<String>> testIamPermissionAsync(String instanceId, String. |
1210 | 1210 | */ |
1211 | 1211 | @SuppressWarnings("WeakerAccess") |
1212 | 1212 | public List<String> testIamPermission(ResourceName resourceName, String... permissions) { |
1213 | | - return awaitFuture(testIamPermissionAsync(resourceName, permissions)); |
| 1213 | + return ApiExceptions.callAndTranslateApiException( |
| 1214 | + testIamPermissionAsync(resourceName, permissions)); |
1214 | 1215 | } |
1215 | 1216 |
|
1216 | 1217 |
|
@@ -1276,29 +1277,4 @@ public com.google.iam.v1.Policy toPb(Policy policy) { |
1276 | 1277 | return super.toPb(policy); |
1277 | 1278 | } |
1278 | 1279 | } |
1279 | | - |
1280 | | - /** |
1281 | | - * Awaits the result of a future, taking care to propagate errors while maintaining the call site |
1282 | | - * in a suppressed exception. This allows semantic errors to be caught across threads, while |
1283 | | - * preserving the call site in the error. The caller's stacktrace will be made available as a |
1284 | | - * suppressed exception. |
1285 | | - */ |
1286 | | - // TODO(igorbernstein2): try to move this into gax |
1287 | | - private <T> T awaitFuture(ApiFuture<T> future) { |
1288 | | - RuntimeException error; |
1289 | | - try { |
1290 | | - return Futures.getUnchecked(future); |
1291 | | - } catch (UncheckedExecutionException e) { |
1292 | | - if (e.getCause() instanceof RuntimeException) { |
1293 | | - error = (RuntimeException) e.getCause(); |
1294 | | - } else { |
1295 | | - error = e; |
1296 | | - } |
1297 | | - } catch (RuntimeException e) { |
1298 | | - error = e; |
1299 | | - } |
1300 | | - // Add the caller's stack as a suppressed exception |
1301 | | - error.addSuppressed(new RuntimeException("Encountered error while awaiting future")); |
1302 | | - throw error; |
1303 | | - } |
1304 | 1280 | } |
0 commit comments