Skip to content

Commit cb878d0

Browse files
committed
By default normalizing error response message only on cases that can effect String.format
1 parent d865612 commit cb878d0

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.taboola.rest.api.exceptions.RestAPIConnectivityException;
44
import com.taboola.rest.api.exceptions.RestAPIRequestException;
55
import com.taboola.rest.api.exceptions.RestAPIUnauthorizedException;
6+
import com.taboola.rest.api.internal.MessageHandlingUtils;
67

78
/**
89
* Created by vladi.m
@@ -19,7 +20,7 @@ public void handleAndThrowUnauthorizedException(Throwable cause) {
1920

2021
@Override
2122
public void handleAndThrowRequestException(int responseCode, byte[] errorPayloadBytes, String message) {
22-
throw new RestAPIRequestException("message: %s, responseCode: %s", message, responseCode);
23+
throw new RestAPIRequestException("message: %s, responseCode: %s", MessageHandlingUtils.normalizeErrorMsg(message), responseCode);
2324
}
2425

2526
@Override
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.taboola.rest.api.internal;
2+
3+
/**
4+
* Created by vladi.m
5+
* Date 28/06/2021
6+
* Time 16:52
7+
* Copyright Taboola
8+
*/
9+
public class MessageHandlingUtils {
10+
11+
public static String normalizeErrorMsg(String message) {
12+
if(message != null) {
13+
return message.replaceAll("%", "%%");
14+
}
15+
16+
return "";
17+
}
18+
}

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public Object adapt(Call<Object> call) {
6565
exceptionFactory.handleAndThrowUnauthorizedException(safeCreateCauseException(response));
6666

6767
} else if(responseCode >= BAD_REQUEST_HTTP_STATUS_CODE && responseCode < INTERNAL_SERVER_ERROR_HTTP_STATUS_CODE) {
68-
String message = normalizeErrorMsg(response.message());
68+
String message = response.message();
6969
exceptionFactory.handleAndThrowRequestException(responseCode, safeGetErrorPayloadBytes(response, message, responseCode), message);
7070
}
7171

@@ -91,7 +91,7 @@ private byte[] safeGetErrorPayloadBytes(Response<Object> response, String messag
9191
return response.errorBody().bytes();
9292
} catch(Throwable t) {
9393
logger.warn("Failed to extract byte[] from response error body", t);
94-
throw new RestAPIRequestException("message: %s, responseCode: %s", message, responseCode);
94+
throw new RestAPIRequestException("message: %s, responseCode: %s", MessageHandlingUtils.normalizeErrorMsg(message), responseCode);
9595
}
9696
}
9797

@@ -103,12 +103,4 @@ private IOException safeCreateCauseException(Response<Object> response) {
103103
return new IOException("Failed to parse API error response", t);
104104
}
105105
}
106-
107-
private String normalizeErrorMsg(String message) {
108-
if(message != null) {
109-
return message.replaceAll("%", "%%");
110-
}
111-
112-
return "";
113-
}
114106
}

0 commit comments

Comments
 (0)