@@ -24,7 +24,7 @@ public class DefaultCmabClient implements CmabClient {
24
24
25
25
private static final Logger logger = LoggerFactory .getLogger (DefaultCmabClient .class );
26
26
// Update constants to match JS error messages format
27
- private static final String CMAB_FETCH_FAILED = "CMAB decision fetch failed with status: %d " ;
27
+ private static final String CMAB_FETCH_FAILED = "CMAB decision fetch failed with status: %s " ;
28
28
private static final String INVALID_CMAB_FETCH_RESPONSE = "Invalid CMAB fetch response" ;
29
29
private static final Pattern VARIATION_ID_PATTERN = Pattern .compile ("\" variation_id\" \\ s*:\\ s*\" ?([^\" \\ s,}]+)\" ?" );
30
30
private static final String CMAB_PREDICTION_ENDPOINT = "https://prediction.cmab.optimizely.com/predict/%s" ;
@@ -83,8 +83,9 @@ private CompletableFuture<String> doFetch(String url, String requestBody, String
83
83
try {
84
84
request .setEntity (new StringEntity (requestBody , StandardCharsets .UTF_8 ));
85
85
} catch (Exception e ) {
86
- logger .error (String .format (CMAB_FETCH_FAILED , e ));
87
- throw new CompletionException (new RuntimeException (String .format (CMAB_FETCH_FAILED , e )));
86
+ String errorMsg = String .format (CMAB_FETCH_FAILED , "encoding error" );
87
+ logger .error (errorMsg + " for user: {}" , userId , e );
88
+ throw new CompletionException (new RuntimeException (errorMsg , e ));
88
89
}
89
90
90
91
CloseableHttpResponse response = null ;
@@ -94,18 +95,20 @@ private CompletableFuture<String> doFetch(String url, String requestBody, String
94
95
95
96
int statusCode = response .getStatusLine ().getStatusCode ();
96
97
98
+ // Status code error
97
99
if (!isSuccessStatusCode (statusCode )) {
98
- logger . error ( "CMAB fetch failed (Response code: {}) for user: {}" , statusCode , userId );
99
- // Status code error (like JS: new OptimizelyError(CMAB_FETCH_FAILED, response.statusCode))
100
- throw new CompletionException (new RuntimeException (String . format ( CMAB_FETCH_FAILED , statusCode ) ));
100
+ String errorMsg = String . format ( CMAB_FETCH_FAILED , statusCode );
101
+ logger . error ( errorMsg + " for user: {}" , userId );
102
+ throw new CompletionException (new RuntimeException (errorMsg ));
101
103
}
102
104
103
105
String responseBody = EntityUtils .toString (response .getEntity (), StandardCharsets .UTF_8 );
104
106
logger .debug ("CMAB response received for user: {}" , userId );
105
107
106
108
109
+ // Invalid response error
107
110
if (!validateResponse (responseBody )) {
108
- logger .error (INVALID_CMAB_FETCH_RESPONSE );
111
+ logger .error (INVALID_CMAB_FETCH_RESPONSE + " for user: {}" , userId );
109
112
throw new CompletionException (new RuntimeException (INVALID_CMAB_FETCH_RESPONSE ));
110
113
}
111
114
@@ -115,8 +118,10 @@ private CompletableFuture<String> doFetch(String url, String requestBody, String
115
118
return variationId ;
116
119
117
120
} catch (IOException e ) {
118
- logger .error (String .format (CMAB_FETCH_FAILED , e ));
119
- throw new CompletionException (new RuntimeException (String .format (CMAB_FETCH_FAILED , e )));
121
+ // IO error
122
+ String errorMsg = String .format (CMAB_FETCH_FAILED , "network error" );
123
+ logger .error (errorMsg + " for user: {}" , userId , e );
124
+ throw new CompletionException (new RuntimeException (errorMsg , e ));
120
125
} finally {
121
126
closeHttpResponse (response );
122
127
}
0 commit comments