Skip to content

Commit 0f9cef2

Browse files
committed
Fix for #27
1 parent 3c5a6c2 commit 0f9cef2

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/main/java/com/taboola/backstage/internal/SynchronousCallAdapterFactory.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public Object adapt(Call<Object> call) {
6767
throw new BackstageAPIUnauthorizedException();
6868

6969
} else if(responseCode >= BAD_REQUEST_HTTP_STATUS_CODE && responseCode < INTERNAL_SERVER_ERROR_HTTP_STATUS_CODE) {
70-
throw new BackstageAPIRequestException(responseCode, parseError(response));
70+
throw new BackstageAPIRequestException(responseCode, normalizeError(parseError(response)));
7171
}
7272

7373
throw new BackstageAPIConnectivityException(responseCode);
@@ -92,4 +92,13 @@ private APIError parseError(Response errorResponse) {
9292
return new APIError(errorResponse.message(), errorResponse.code());
9393
}
9494
}
95+
96+
APIError normalizeError(APIError error) {
97+
String message = error.getMessage();
98+
if(message != null) {
99+
error.setMessage(message.replaceAll("%", "%%"));
100+
}
101+
102+
return error;
103+
}
95104
}

src/test/java/com/taboola/backstage/internal/SynchronousCallAdapterFactoryTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package com.taboola.backstage.internal;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.taboola.backstage.exceptions.BackstageAPIRequestException;
5+
import com.taboola.backstage.model.APIError;
46
import com.taboola.backstage.model.media.campaigns.Campaign;
7+
8+
import org.apache.logging.log4j.LogManager;
9+
import org.apache.logging.log4j.Logger;
510
import org.junit.Assert;
611
import org.junit.Before;
712
import org.junit.Test;
813
import retrofit2.Call;
914
import retrofit2.CallAdapter;
15+
import static org.junit.Assert.assertEquals;
16+
1017
import com.taboola.backstage.BackstageTestBase;
1118

1219
/**
@@ -17,6 +24,7 @@
1724
*/
1825
public class SynchronousCallAdapterFactoryTest extends BackstageTestBase {
1926

27+
private static final Logger logger = LogManager.getLogger(SynchronousCallAdapterFactoryTest.class);
2028
private SynchronousCallAdapterFactory testInstance;
2129

2230
@Before
@@ -41,5 +49,14 @@ public void testSynchronousAdapterForCallType() {
4149
Assert.assertNull("Invalid adapter, expecting no adapter", actualAdapter);
4250
}
4351

52+
@Test
53+
public void testParseError_whenErrorMsgContainsPercentSign_expectingValidFormatForStringFormat() {
54+
APIError apiError = new APIError("Cpc boost value must be between -99% and 100%", 400);
55+
apiError = testInstance.normalizeError(apiError);
56+
BackstageAPIRequestException ex = new BackstageAPIRequestException(400, apiError);
57+
logger.info(ex);
58+
assertEquals("Failed to perform API call with response code [400]. Response payload status [400], message [Cpc boost value must be between -99% and 100%], offending field [null]", ex.getMessage());
59+
}
60+
4461
//TODO add more tests - retrofit2#Response.class is final therefore mockito is not working on it
4562
}

0 commit comments

Comments
 (0)