11package com.taboola.rest.api.internal;
22
33import com.taboola.rest.api.exceptions.RestAPIConnectivityException;
4+ import com.taboola.rest.api.exceptions.RestAPIRequestException;
45import com.taboola.rest.api.exceptions.factories.ExceptionFactory;
56
67import org.apache.logging.log4j.LogManager;
@@ -64,7 +65,8 @@ public Object adapt(Call<Object> call) {
6465 exceptionFactory.handleAndThrowUnauthorizedException(safeCreateCauseException(response));
6566
6667 } else if(responseCode >= BAD_REQUEST_HTTP_STATUS_CODE && responseCode < INTERNAL_SERVER_ERROR_HTTP_STATUS_CODE) {
67- exceptionFactory.handleAndThrowRequestException(responseCode, safeGetErrorPayloadBytes(response), normalizeErrorMsg(response.message()));
68+ String message = normalizeErrorMsg(response.message());
69+ exceptionFactory.handleAndThrowRequestException(responseCode, safeGetErrorPayloadBytes(response, message, responseCode), message);
6870 }
6971
7072 exceptionFactory.handleAndThrowConnectivityException(safeCreateCauseException(response), responseCode);
@@ -84,12 +86,12 @@ public Object adapt(Call<Object> call) {
8486 };
8587 }
8688
87- private byte[] safeGetErrorPayloadBytes(Response<Object> response) throws IOException {
89+ private byte[] safeGetErrorPayloadBytes(Response<Object> response, String message, int responseCode ) throws IOException {
8890 try {
8991 return response.errorBody().bytes();
9092 } catch(Throwable t) {
9193 logger.warn("Failed to extract byte[] from response error body", t);
92- throw new RestAPIConnectivityException( );
94+ throw new RestAPIRequestException("message: %s, responseCode: %s", message, responseCode );
9395 }
9496 }
9597
0 commit comments