Skip to content

Commit 0910fe2

Browse files
committed
Converting to factory of exception and not handler
1 parent cb878d0 commit 0910fe2

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

src/main/java/com/taboola/rest/api/exceptions/factories/DefaultExceptionFactory.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@
1414
public class DefaultExceptionFactory implements ExceptionFactory {
1515

1616
@Override
17-
public void handleAndThrowUnauthorizedException(Throwable cause) {
18-
throw new RestAPIUnauthorizedException(cause);
17+
public RuntimeException createUnauthorizedException(Throwable cause) {
18+
return new RestAPIUnauthorizedException(cause);
1919
}
2020

2121
@Override
22-
public void handleAndThrowRequestException(int responseCode, byte[] errorPayloadBytes, String message) {
23-
throw new RestAPIRequestException("message: %s, responseCode: %s", MessageHandlingUtils.normalizeErrorMsg(message), responseCode);
22+
public RuntimeException createRequestException(int responseCode, byte[] errorPayloadBytes, String message) {
23+
return new RestAPIRequestException("message: %s, responseCode: %s", MessageHandlingUtils.normalizeErrorMsg(message), responseCode);
2424
}
2525

2626
@Override
27-
public void handleAndThrowConnectivityException(Throwable cause, int responseCode) {
28-
throw new RestAPIConnectivityException(cause, responseCode);
27+
public RuntimeException createConnectivityException(Throwable cause, int responseCode) {
28+
return new RestAPIConnectivityException(cause, responseCode);
2929
}
3030

3131
@Override
32-
public void handleAndThrowConnectivityException(Throwable cause) {
33-
throw new RestAPIConnectivityException(cause);
32+
public RuntimeException createConnectivityException(Throwable cause) {
33+
return new RestAPIConnectivityException(cause);
3434
}
3535
}

src/main/java/com/taboola/rest/api/exceptions/factories/ExceptionFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
*/
99
public interface ExceptionFactory {
1010

11-
void handleAndThrowUnauthorizedException(Throwable cause);
11+
RuntimeException createUnauthorizedException(Throwable cause);
1212

13-
void handleAndThrowRequestException(int responseCode, byte[] errorBytes, String message);
13+
RuntimeException createRequestException(int responseCode, byte[] errorBytes, String message);
1414

15-
void handleAndThrowConnectivityException(Throwable cause, int responseCode);
15+
RuntimeException createConnectivityException(Throwable cause, int responseCode);
1616

17-
void handleAndThrowConnectivityException(Throwable cause);
17+
RuntimeException createConnectivityException(Throwable cause);
1818
}

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,19 @@ public Object adapt(Call<Object> call) {
6262
} else {
6363
int responseCode = response.code();
6464
if(responseCode == UNAUTHORIZED_HTTP_STATUS_CODE) {
65-
exceptionFactory.handleAndThrowUnauthorizedException(safeCreateCauseException(response));
65+
throwIfNotNull(exceptionFactory.createUnauthorizedException(safeCreateCauseException(response)));
6666

6767
} else if(responseCode >= BAD_REQUEST_HTTP_STATUS_CODE && responseCode < INTERNAL_SERVER_ERROR_HTTP_STATUS_CODE) {
6868
String message = response.message();
69-
exceptionFactory.handleAndThrowRequestException(responseCode, safeGetErrorPayloadBytes(response, message, responseCode), message);
69+
throwIfNotNull(exceptionFactory.createRequestException(responseCode, safeGetErrorPayloadBytes(response, message, responseCode), message));
7070
}
7171

72-
exceptionFactory.handleAndThrowConnectivityException(safeCreateCauseException(response), responseCode);
72+
throwIfNotNull(exceptionFactory.createConnectivityException(safeCreateCauseException(response), responseCode));
7373
}
7474

7575
} catch (IOException e) {
7676
logger.error(e);
77-
exceptionFactory.handleAndThrowConnectivityException(e);
77+
throwIfNotNull(exceptionFactory.createConnectivityException(e));
7878
}
7979

8080
if(obj == null) {
@@ -103,4 +103,10 @@ private IOException safeCreateCauseException(Response<Object> response) {
103103
return new IOException("Failed to parse API error response", t);
104104
}
105105
}
106+
107+
private void throwIfNotNull(RuntimeException e) {
108+
if(e != null) {
109+
throw e;
110+
}
111+
}
106112
}

0 commit comments

Comments
 (0)