Skip to content

Commit 29987a4

Browse files
committed
Added ability to define exceptions when using SynchronousCallAdapter
1 parent 3bd7711 commit 29987a4

File tree

8 files changed

+108
-22
lines changed

8 files changed

+108
-22
lines changed

src/main/java/com/taboola/rest/api/RestAPIClient.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.apache.logging.log4j.LogManager;
1010
import org.apache.logging.log4j.Logger;
1111

12+
import com.taboola.rest.api.exceptions.factories.DefaultExceptionFactory;
13+
import com.taboola.rest.api.exceptions.factories.ExceptionFactory;
1214
import com.taboola.rest.api.internal.CommunicationFactory;
1315
import com.taboola.rest.api.internal.config.CommunicationConfig;
1416
import com.taboola.rest.api.internal.config.SerializationConfig;
@@ -49,6 +51,7 @@ public static class RestAPIClientBuilder {
4951
private static final String DEFAULT_REST_API_VERSION = "UNDEFINED";
5052
private static final String DEFAULT_USER_AGENT_SUFFIX = "UNDEFINED";
5153
private static final String DEFAULT_USER_AGENT_PREFIX = "UNDEFINED";
54+
private static final ExceptionFactory DEFAULT_EXCEPTION_FACTORY = new DefaultExceptionFactory();
5255

5356
private String baseUrl;
5457
private Long writeTimeoutMillis;
@@ -62,6 +65,7 @@ public static class RestAPIClientBuilder {
6265
private String userAgentPrefix;
6366
private String userAgentSuffix;
6467
private String restAPIVersion;
68+
private ExceptionFactory exceptionFactory;
6569

6670
public RestAPIClientBuilder setBaseUrl(String baseUrl) {
6771
this.baseUrl = baseUrl;
@@ -123,12 +127,17 @@ public RestAPIClientBuilder setAPIVersion(String restAPIVersion) {
123127
return this;
124128
}
125129

130+
public RestAPIClientBuilder setExceptionFactory(ExceptionFactory exceptionFactory) {
131+
this.exceptionFactory = exceptionFactory;
132+
return this;
133+
}
134+
126135
public RestAPIClient build() {
127136
organizeState();
128137
String finalUserAgent = String.format("%s/%s/%s (%s)", userAgentPrefix, restAPIVersion, VERSION, userAgentSuffix);
129138
Collection<RequestHeader> headers = getAllHeaders(this.headers, finalUserAgent);
130139
CommunicationConfig config = new CommunicationConfig(baseUrl, connectionTimeoutMillis, readTimeoutMillis, writeTimeoutMillis, maxIdleConnections,
131-
keepAliveDurationMillis, headers, debug);
140+
keepAliveDurationMillis, headers, debug, exceptionFactory);
132141
return new RestAPIClient(new CommunicationFactory(config, serializationConfig));
133142
}
134143

@@ -187,6 +196,10 @@ private void organizeState() {
187196
if (serializationConfig == null) {
188197
serializationConfig = DEFAULT_SERIALIZATION_CONFIG;
189198
}
199+
200+
if(exceptionFactory == null) {
201+
exceptionFactory = DEFAULT_EXCEPTION_FACTORY;
202+
}
190203
}
191204
}
192205

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.taboola.rest.api.exceptions.factories;
2+
3+
import com.taboola.rest.api.exceptions.RestAPIConnectivityException;
4+
import com.taboola.rest.api.exceptions.RestAPIRequestException;
5+
import com.taboola.rest.api.exceptions.RestAPIUnauthorizedException;
6+
import com.taboola.rest.api.model.APIError;
7+
8+
/**
9+
* Created by vladi.m
10+
* Date 28/06/2021
11+
* Time 12:10
12+
* Copyright Taboola
13+
*/
14+
public class DefaultExceptionFactory implements ExceptionFactory {
15+
16+
@Override
17+
public void handleAndThrowUnauthorizedException(Throwable cause) {
18+
throw new RestAPIUnauthorizedException(cause);
19+
}
20+
21+
@Override
22+
public void handleAndThrowRequestException(int responseCode, APIError error) {
23+
throw new RestAPIRequestException(responseCode, error);
24+
}
25+
26+
@Override
27+
public void handleAndThrowConnectivityException(Throwable cause, int responseCode) {
28+
throw new RestAPIConnectivityException(cause, responseCode);
29+
}
30+
31+
@Override
32+
public void handleAndThrowConnectivityException(Throwable cause) {
33+
throw new RestAPIConnectivityException(cause);
34+
}
35+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.taboola.rest.api.exceptions.factories;
2+
3+
import com.taboola.rest.api.model.APIError;
4+
5+
/**
6+
* Created by vladi.m
7+
* Date 28/06/2021
8+
* Time 12:06
9+
* Copyright Taboola
10+
*/
11+
public interface ExceptionFactory {
12+
13+
void handleAndThrowUnauthorizedException(Throwable cause);
14+
15+
void handleAndThrowRequestException(int responseCode, APIError error);
16+
17+
void handleAndThrowConnectivityException(Throwable cause, int responseCode);
18+
19+
void handleAndThrowConnectivityException(Throwable cause);
20+
}

src/main/java/com/taboola/rest/api/internal/CommunicationFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private Retrofit.Builder createRetrofitBuilder(CommunicationConfig config) {
5050
return new Retrofit.Builder()
5151
.addConverterFactory(StringConverterFactory.create())
5252
.addConverterFactory(JacksonConverterFactory.create(objectMapper))
53-
.addCallAdapterFactory(SynchronousCallAdapterFactory.create(objectMapper))
53+
.addCallAdapterFactory(SynchronousCallAdapterFactory.create(objectMapper, config.getExceptionFactory()))
5454
.client(createOkHttpClient(config));
5555
}
5656

src/main/java/com/taboola/rest/api/internal/SynchronousCallAdapterFactory.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.taboola.rest.api.exceptions.RestAPIConnectivityException;
55
import com.taboola.rest.api.exceptions.RestAPIRequestException;
66
import com.taboola.rest.api.exceptions.RestAPIUnauthorizedException;
7+
import com.taboola.rest.api.exceptions.factories.ExceptionFactory;
78
import com.taboola.rest.api.model.APIError;
89

910
import okhttp3.ResponseBody;
@@ -32,13 +33,15 @@ public class SynchronousCallAdapterFactory extends CallAdapter.Factory {
3233
private static final int INTERNAL_SERVER_ERROR_HTTP_STATUS_CODE = 500;
3334

3435
private final ObjectMapper objectMapper;
36+
private final ExceptionFactory exceptionFactory;
3537

36-
public static SynchronousCallAdapterFactory create(ObjectMapper objectMapper) {
37-
return new SynchronousCallAdapterFactory(objectMapper);
38+
public static SynchronousCallAdapterFactory create(ObjectMapper objectMapper, ExceptionFactory exceptionFactory) {
39+
return new SynchronousCallAdapterFactory(objectMapper, exceptionFactory);
3840
}
3941

40-
private SynchronousCallAdapterFactory(ObjectMapper objectMapper) {
42+
private SynchronousCallAdapterFactory(ObjectMapper objectMapper, ExceptionFactory exceptionFactory) {
4143
this.objectMapper = objectMapper;
44+
this.exceptionFactory = exceptionFactory;
4245
}
4346

4447
@Override
@@ -57,26 +60,30 @@ public Type responseType() {
5760

5861
@Override
5962
public Object adapt(Call<Object> call) {
60-
Object obj;
63+
Object obj = null;
6164
try {
6265
Response<Object> response = call.execute();
6366
if(response.isSuccessful()) {
6467
obj = response.body();
6568
} else {
6669
int responseCode = response.code();
6770
if(responseCode == UNAUTHORIZED_HTTP_STATUS_CODE) {
68-
throw new RestAPIUnauthorizedException(safeCreateCauseException(response));
71+
exceptionFactory.handleAndThrowUnauthorizedException(safeCreateCauseException(response));
6972

7073
} else if(responseCode >= BAD_REQUEST_HTTP_STATUS_CODE && responseCode < INTERNAL_SERVER_ERROR_HTTP_STATUS_CODE) {
71-
throw new RestAPIRequestException(responseCode, normalizeError(parseError(response)));
74+
exceptionFactory.handleAndThrowRequestException(responseCode, normalizeError(parseError(response)));
7275
}
7376

74-
throw new RestAPIConnectivityException(safeCreateCauseException(response), responseCode);
77+
exceptionFactory.handleAndThrowConnectivityException(safeCreateCauseException(response), responseCode);
7578
}
7679

7780
} catch (IOException e) {
7881
logger.error(e);
79-
throw new RestAPIConnectivityException(e);
82+
exceptionFactory.handleAndThrowConnectivityException(e);
83+
}
84+
85+
if(obj == null) {
86+
throw new RestAPIConnectivityException();
8087
}
8188

8289
return obj;

src/main/java/com/taboola/rest/api/internal/config/CommunicationConfig.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Collection;
44

5+
import com.taboola.rest.api.exceptions.factories.ExceptionFactory;
56
import com.taboola.rest.api.model.RequestHeader;
67

78
/**
@@ -20,9 +21,11 @@ public class CommunicationConfig {
2021
private final long keepAliveDurationMillis;
2122
private final boolean debug;
2223
private final Collection<RequestHeader> headers;
24+
private final ExceptionFactory exceptionFactory;
2325

2426
public CommunicationConfig(String baseUrl, Long connectionTimeoutMillis, Long readTimeoutMillis,
25-
Long writeTimeoutMillis, Integer maxIdleConnections, Long keepAliveDurationMillis, Collection<RequestHeader> headers, boolean debug) {
27+
Long writeTimeoutMillis, Integer maxIdleConnections, Long keepAliveDurationMillis,
28+
Collection<RequestHeader> headers, boolean debug, ExceptionFactory exceptionFactory) {
2629
this.baseUrl = baseUrl;
2730
this.connectionTimeoutMillis = connectionTimeoutMillis;
2831
this.readTimeoutMillis = readTimeoutMillis;
@@ -31,6 +34,7 @@ public CommunicationConfig(String baseUrl, Long connectionTimeoutMillis, Long re
3134
this.keepAliveDurationMillis = keepAliveDurationMillis;
3235
this.headers = headers;
3336
this.debug = debug;
37+
this.exceptionFactory = exceptionFactory;
3438
}
3539

3640
public String getBaseUrl() {
@@ -65,17 +69,22 @@ public boolean isDebug() {
6569
return debug;
6670
}
6771

72+
public ExceptionFactory getExceptionFactory() {
73+
return exceptionFactory;
74+
}
75+
6876
@Override
6977
public String toString() {
7078
return "CommunicationConfig{" +
71-
"baseUrl='" + baseUrl + '\'' +
72-
", connectionTimeoutMillis=" + connectionTimeoutMillis +
73-
", readTimeoutMillis=" + readTimeoutMillis +
74-
", writeTimeoutMillis=" + writeTimeoutMillis +
75-
", maxIdleConnections=" + maxIdleConnections +
76-
", keepAliveDurationMillis=" + keepAliveDurationMillis +
77-
", headers='" + headers + '\'' +
78-
", debug=" + debug +
79-
'}';
79+
"baseUrl='" + baseUrl + '\'' +
80+
", connectionTimeoutMillis=" + connectionTimeoutMillis +
81+
", readTimeoutMillis=" + readTimeoutMillis +
82+
", writeTimeoutMillis=" + writeTimeoutMillis +
83+
", maxIdleConnections=" + maxIdleConnections +
84+
", keepAliveDurationMillis=" + keepAliveDurationMillis +
85+
", debug=" + debug +
86+
", headers=" + headers +
87+
", exceptionFactory=" + exceptionFactory +
88+
'}';
8089
}
8190
}

src/test/java/com/taboola/rest/api/internal/CommunicationFactoryTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.junit.Test;
1212

1313
import com.taboola.rest.api.exceptions.RestAPIException;
14+
import com.taboola.rest.api.exceptions.factories.DefaultExceptionFactory;
1415
import com.taboola.rest.api.internal.config.CommunicationConfig;
1516
import com.taboola.rest.api.internal.config.SerializationConfig;
1617
import com.taboola.rest.api.internal.config.UserAgentHeader;
@@ -35,7 +36,7 @@ public interface TestEndpoint {
3536
public void beforeTest() {
3637
CommunicationConfig communicationConfig = new CommunicationConfig("http://localhost",
3738
1L, 1L, 1L, 1, 60L,
38-
Collections.singleton(new UserAgentHeader("Dummy-Agent")),true);
39+
Collections.singleton(new UserAgentHeader("Dummy-Agent")),true, new DefaultExceptionFactory());
3940
SerializationConfig serializationConfig = new SerializationConfig();
4041
testInstance = new CommunicationFactory(communicationConfig, serializationConfig);
4142
}

src/test/java/com/taboola/rest/api/internal/SynchronousCallAdapterFactoryTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import com.fasterxml.jackson.databind.ObjectMapper;
1010
import com.taboola.rest.api.exceptions.RestAPIRequestException;
11+
import com.taboola.rest.api.exceptions.factories.DefaultExceptionFactory;
1112
import com.taboola.rest.api.model.APIError;
1213

1314
import retrofit2.Call;
@@ -29,7 +30,7 @@ public static class DummyTestClass { }
2930

3031
@Before
3132
public void beforeTest() {
32-
testInstance = SynchronousCallAdapterFactory.create(new ObjectMapper());
33+
testInstance = SynchronousCallAdapterFactory.create(new ObjectMapper(), new DefaultExceptionFactory());
3334
}
3435

3536
@Test

0 commit comments

Comments
 (0)