Skip to content

Commit cdf0e94

Browse files
committed
Move exception handler and interceptor to BaseService class
1 parent f350a54 commit cdf0e94

File tree

5 files changed

+26
-97
lines changed

5 files changed

+26
-97
lines changed

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryImpl.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
import com.google.common.collect.Lists;
3535
import com.google.common.collect.Maps;
3636
import com.google.gcloud.BaseService;
37-
import com.google.gcloud.ExceptionHandler;
38-
import com.google.gcloud.ExceptionHandler.Interceptor;
3937
import com.google.gcloud.Page;
4038
import com.google.gcloud.PageImpl;
4139
import com.google.gcloud.PageImpl.NextPageFetcher;
@@ -49,27 +47,6 @@
4947

5048
final class BigQueryImpl extends BaseService<BigQueryOptions> implements BigQuery {
5149

52-
private static final Interceptor EXCEPTION_HANDLER_INTERCEPTOR = new Interceptor() {
53-
54-
private static final long serialVersionUID = -7478333733015750774L;
55-
56-
@Override
57-
public RetryResult afterEval(Exception exception, RetryResult retryResult) {
58-
return Interceptor.RetryResult.CONTINUE_EVALUATION;
59-
}
60-
61-
@Override
62-
public RetryResult beforeEval(Exception exception) {
63-
if (exception instanceof BigQueryException) {
64-
boolean retriable = ((BigQueryException) exception).retryable();
65-
return retriable ? Interceptor.RetryResult.RETRY : Interceptor.RetryResult.NO_RETRY;
66-
}
67-
return Interceptor.RetryResult.CONTINUE_EVALUATION;
68-
}
69-
};
70-
static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.builder()
71-
.abortOn(RuntimeException.class).interceptor(EXCEPTION_HANDLER_INTERCEPTOR).build();
72-
7350
private static class DatasetPageFetcher implements NextPageFetcher<DatasetInfo> {
7451

7552
private static final long serialVersionUID = -3057564042439021278L;

gcloud-java-core/src/main/java/com/google/gcloud/BaseService.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.gcloud;
1818

19+
import com.google.gcloud.ExceptionHandler.Interceptor;
20+
1921
/**
2022
* Base class for service objects.
2123
*
@@ -24,6 +26,29 @@
2426
public abstract class BaseService<OptionsT extends ServiceOptions<?, ?, OptionsT>>
2527
implements Service<OptionsT> {
2628

29+
public static final Interceptor EXCEPTION_HANDLER_INTERCEPTOR = new Interceptor() {
30+
31+
private static final long serialVersionUID = -8429573486870467828L;
32+
33+
@Override
34+
public RetryResult afterEval(Exception exception, RetryResult retryResult) {
35+
return Interceptor.RetryResult.CONTINUE_EVALUATION;
36+
}
37+
38+
@Override
39+
public RetryResult beforeEval(Exception exception) {
40+
if (exception instanceof BaseServiceException) {
41+
boolean retriable = ((BaseServiceException) exception).retryable();
42+
return retriable ? Interceptor.RetryResult.RETRY : Interceptor.RetryResult.NO_RETRY;
43+
}
44+
return Interceptor.RetryResult.CONTINUE_EVALUATION;
45+
}
46+
};
47+
public static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.builder()
48+
.abortOn(RuntimeException.class)
49+
.interceptor(EXCEPTION_HANDLER_INTERCEPTOR)
50+
.build();
51+
2752
private final OptionsT options;
2853

2954
protected BaseService(OptionsT options) {

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import com.google.common.collect.ImmutableList;
2424
import com.google.common.collect.Sets;
2525
import com.google.gcloud.BaseService;
26-
import com.google.gcloud.ExceptionHandler;
27-
import com.google.gcloud.ExceptionHandler.Interceptor;
2826
import com.google.gcloud.RetryHelper;
2927
import com.google.gcloud.RetryHelper.RetryHelperException;
3028
import com.google.gcloud.RetryParams;
@@ -41,31 +39,7 @@
4139
import java.util.Set;
4240
import java.util.concurrent.Callable;
4341

44-
final class DatastoreImpl extends BaseService<DatastoreOptions>
45-
implements Datastore {
46-
47-
private static final Interceptor EXCEPTION_HANDLER_INTERCEPTOR =
48-
new Interceptor() {
49-
50-
private static final long serialVersionUID = 6911242958397733203L;
51-
52-
@Override
53-
public RetryResult afterEval(Exception exception, RetryResult retryResult) {
54-
return Interceptor.RetryResult.CONTINUE_EVALUATION;
55-
}
56-
57-
@Override
58-
public RetryResult beforeEval(Exception exception) {
59-
if (exception instanceof DatastoreException) {
60-
boolean retryable = ((DatastoreException) exception).retryable();
61-
return retryable ? Interceptor.RetryResult.RETRY : Interceptor.RetryResult.NO_RETRY;
62-
}
63-
return Interceptor.RetryResult.CONTINUE_EVALUATION;
64-
}
65-
};
66-
private static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.builder()
67-
.abortOn(RuntimeException.class, DatastoreException.class)
68-
.interceptor(EXCEPTION_HANDLER_INTERCEPTOR).build();
42+
final class DatastoreImpl extends BaseService<DatastoreOptions> implements Datastore {
6943

7044
private final DatastoreRpc datastoreRpc;
7145
private final RetryParams retryParams;

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
import com.google.common.collect.Iterables;
2626
import com.google.common.collect.Maps;
2727
import com.google.gcloud.BaseService;
28-
import com.google.gcloud.ExceptionHandler;
29-
import com.google.gcloud.ExceptionHandler.Interceptor;
3028
import com.google.gcloud.Page;
3129
import com.google.gcloud.PageImpl;
3230
import com.google.gcloud.PageImpl.NextPageFetcher;
@@ -40,29 +38,6 @@
4038
final class ResourceManagerImpl
4139
extends BaseService<ResourceManagerOptions> implements ResourceManager {
4240

43-
private static final Interceptor EXCEPTION_HANDLER_INTERCEPTOR = new Interceptor() {
44-
45-
private static final long serialVersionUID = 2091576149969931704L;
46-
47-
@Override
48-
public RetryResult afterEval(Exception exception, RetryResult retryResult) {
49-
return Interceptor.RetryResult.CONTINUE_EVALUATION;
50-
}
51-
52-
@Override
53-
public RetryResult beforeEval(Exception exception) {
54-
if (exception instanceof ResourceManagerException) {
55-
boolean retriable = ((ResourceManagerException) exception).retryable();
56-
return retriable ? Interceptor.RetryResult.RETRY : Interceptor.RetryResult.NO_RETRY;
57-
}
58-
return Interceptor.RetryResult.CONTINUE_EVALUATION;
59-
}
60-
};
61-
static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.builder()
62-
.abortOn(RuntimeException.class)
63-
.interceptor(EXCEPTION_HANDLER_INTERCEPTOR)
64-
.build();
65-
6641
private final ResourceManagerRpc resourceManagerRpc;
6742

6843
ResourceManagerImpl(ResourceManagerOptions options) {

gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
import com.google.common.primitives.Ints;
4545
import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials;
4646
import com.google.gcloud.BaseService;
47-
import com.google.gcloud.ExceptionHandler;
48-
import com.google.gcloud.ExceptionHandler.Interceptor;
4947
import com.google.gcloud.Page;
5048
import com.google.gcloud.PageImpl;
5149
import com.google.gcloud.PageImpl.NextPageFetcher;
@@ -75,26 +73,6 @@
7573

7674
final class StorageImpl extends BaseService<StorageOptions> implements Storage {
7775

78-
private static final Interceptor EXCEPTION_HANDLER_INTERCEPTOR = new Interceptor() {
79-
80-
private static final long serialVersionUID = -7758580330857881124L;
81-
82-
@Override
83-
public RetryResult afterEval(Exception exception, RetryResult retryResult) {
84-
return Interceptor.RetryResult.CONTINUE_EVALUATION;
85-
}
86-
87-
@Override
88-
public RetryResult beforeEval(Exception exception) {
89-
if (exception instanceof StorageException) {
90-
boolean retriable = ((StorageException) exception).retryable();
91-
return retriable ? Interceptor.RetryResult.RETRY : Interceptor.RetryResult.NO_RETRY;
92-
}
93-
return Interceptor.RetryResult.CONTINUE_EVALUATION;
94-
}
95-
};
96-
static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.builder()
97-
.abortOn(RuntimeException.class).interceptor(EXCEPTION_HANDLER_INTERCEPTOR).build();
9876
private static final byte[] EMPTY_BYTE_ARRAY = {};
9977
private static final String EMPTY_BYTE_ARRAY_MD5 = "1B2M2Y8AsgTpgAmY7PhCfg==";
10078
private static final String EMPTY_BYTE_ARRAY_CRC32C = "AAAAAA==";

0 commit comments

Comments
 (0)