30
30
import com .vwo .services .LoggerService ;
31
31
import com .vwo .services .UrlService ;
32
32
import com .vwo .utils .DataTypeUtil ;
33
- import com .vwo .utils .SDKMetaUtil ;
34
33
import com .vwo .utils .SettingsUtil ;
35
34
import com .vwo .services .BatchEventQueue ;
36
35
import com .vwo .services .SettingsManager ;
@@ -60,8 +59,6 @@ public VWOClient(String settings, VWOInitOptions options) {
60
59
SettingsUtil .processSettings (this .processedSettings );
61
60
// init url version with collection prefix
62
61
UrlService .init (this .processedSettings .getCollectionPrefix ());
63
- // init SDKMetaUtil and set sdkVersion
64
- SDKMetaUtil .init ();
65
62
LoggerService .log (LogLevelEnum .INFO , "CLIENT_INITIALIZED" , null );
66
63
} catch (Exception exception ) {
67
64
LoggerService .log (LogLevelEnum .ERROR , "exception occurred while parsing settings " + exception .getMessage ());
@@ -101,8 +98,7 @@ public GetFlag getFlag(String featureKey, VWOContext context) {
101
98
throw new IllegalArgumentException ("Feature Key is required" );
102
99
}
103
100
104
- if (this .processedSettings == null || !new SettingsSchema ().isSettingsValid (this .processedSettings )) {
105
- LoggerService .log (LogLevelEnum .ERROR , "SETTINGS_SCHEMA_INVALID" , null );
101
+ if (!validateSettings (this .processedSettings )) {
106
102
getFlag .setIsEnabled (false );
107
103
return getFlag ;
108
104
}
@@ -111,7 +107,7 @@ public GetFlag getFlag(String featureKey, VWOContext context) {
111
107
} catch (Exception exception ) {
112
108
LoggerService .log (LogLevelEnum .ERROR , "API_THROW_ERROR" , new HashMap <String , String >() {{
113
109
put ("apiName" , "getFlag" );
114
- put ("err" , exception .toString ());
110
+ put ("err" , exception .getMessage ());
115
111
}});
116
112
getFlag .setIsEnabled (false );
117
113
return getFlag ;
@@ -147,8 +143,7 @@ private Map<String, Boolean> track(String eventName, VWOContext context, Map<Str
147
143
throw new IllegalArgumentException ("User ID is required" );
148
144
}
149
145
150
- if (this .processedSettings == null || !new SettingsSchema ().isSettingsValid (this .processedSettings )) {
151
- LoggerService .log (LogLevelEnum .ERROR , "SETTINGS_SCHEMA_INVALID" , null );
146
+ if (!validateSettings (this .processedSettings )) {
152
147
resultMap .put (eventName , false );
153
148
return resultMap ;
154
149
}
@@ -163,7 +158,7 @@ private Map<String, Boolean> track(String eventName, VWOContext context, Map<Str
163
158
} catch (Exception exception ) {
164
159
LoggerService .log (LogLevelEnum .ERROR , "API_THROW_ERROR" , new HashMap <String , String >() {{
165
160
put ("apiName" , apiName );
166
- put ("err" , exception .toString ());
161
+ put ("err" , exception .getMessage ());
167
162
}});
168
163
resultMap .put (eventName , false );
169
164
return resultMap ;
@@ -228,16 +223,15 @@ public void setAttribute(Map<String, Object> attributeMap, VWOContext context) {
228
223
throw new IllegalArgumentException ("User ID is required" );
229
224
}
230
225
231
- if (this .processedSettings == null || !new SettingsSchema ().isSettingsValid (this .processedSettings )) {
232
- LoggerService .log (LogLevelEnum .ERROR , "SETTINGS_SCHEMA_INVALID" , null );
226
+ if (!validateSettings (this .processedSettings )) {
233
227
return ;
234
228
}
235
229
236
230
SetAttributeAPI .setAttribute (this .processedSettings , attributeMap , context );
237
231
} catch (Exception exception ) {
238
232
LoggerService .log (LogLevelEnum .ERROR , "API_THROW_ERROR" , new HashMap <String , String >() {{
239
233
put ("apiName" , apiName );
240
- put ("err" , exception .toString ());
234
+ put ("err" , exception .getMessage ());
241
235
}});
242
236
}
243
237
}
@@ -287,15 +281,16 @@ private void updateSettingsOnVWOClient(String newSettings) {
287
281
throw new IllegalArgumentException ("Settings cannot be empty" );
288
282
}
289
283
// Read the new settings and update the processedSettings
290
- this .processedSettings = objectMapper .readValue (newSettings , Settings .class );
291
-
284
+ Settings newProcessedSettings = objectMapper .readValue (newSettings , Settings .class );
292
285
// Check if the new settings are valid
293
- boolean settingsValid = new SettingsSchema ().isSettingsValid (this .processedSettings );
294
- if (settingsValid ) {
286
+ SettingsSchema validationResult = new SettingsSchema ().validateSettings (newProcessedSettings );
287
+ if (validationResult .isValid ()) {
288
+ this .processedSettings = newProcessedSettings ;
289
+ settings = newSettings ;
295
290
// Process the new settings and update the client instance
296
291
SettingsUtil .processSettings (this .processedSettings );
297
292
} else {
298
- throw new IllegalStateException ("Settings schema is invalid" );
293
+ throw new IllegalStateException ("Settings schema is invalid: " + validationResult . getErrorsAsString () );
299
294
}
300
295
} catch (Exception exception ) {
301
296
throw new IllegalStateException (exception .getMessage ());
@@ -314,6 +309,7 @@ public String updateSettings() {
314
309
* @param settings New settings to be updated
315
310
*/
316
311
public String updateSettings (String settings ) {
312
+ Boolean isViaWebhook = false ;
317
313
String apiName = "updateSettings" ;
318
314
try {
319
315
LoggerService .log (LogLevelEnum .DEBUG , "API_CALLED" , new HashMap <String , String >() {{
@@ -322,7 +318,8 @@ public String updateSettings(String settings) {
322
318
323
319
String settingsToUpdate = settings ;
324
320
if (settings == null || settings .isEmpty ()) {
325
- settingsToUpdate = this .updateSettings (true );
321
+ isViaWebhook = true ;
322
+ settingsToUpdate = this .updateSettings (isViaWebhook );
326
323
}
327
324
// Update the settings on the VWOClient instance
328
325
this .updateSettingsOnVWOClient (settingsToUpdate );
@@ -331,9 +328,11 @@ public String updateSettings(String settings) {
331
328
}});
332
329
return settingsToUpdate ;
333
330
} catch (Exception exception ) {
331
+ Boolean finalIsViaWebhook = isViaWebhook ;
334
332
LoggerService .log (LogLevelEnum .ERROR , "SETTINGS_FETCH_FAILED" , new HashMap <String , String >() {{
335
333
put ("apiName" , apiName );
336
- put ("err" , exception .toString ());
334
+ put ("err" , exception .getMessage ());
335
+ put ("isViaWebhook" , finalIsViaWebhook .toString ());
337
336
}});
338
337
return null ;
339
338
}
@@ -353,9 +352,48 @@ public String updateSettings(Boolean isViaWebhook) {
353
352
LoggerService .log (LogLevelEnum .ERROR , "SETTINGS_FETCH_FAILED" , new HashMap <String , String >() {{
354
353
put ("apiName" , apiName );
355
354
put ("isViaWebhook" , isViaWebhook .toString ());
356
- put ("err" , exception .toString ());
355
+ put ("err" , exception .getMessage ());
357
356
}});
358
357
return null ;
359
358
}
360
359
}
361
- }
360
+
361
+
362
+ /**
363
+ * This method is used to validate the settings
364
+ * @param settings Settings to be validated
365
+ * @return Boolean value indicating if the settings are valid
366
+ */
367
+ private Boolean validateSettings (Settings processedSettings ) {
368
+ try {
369
+ if (processedSettings == null ) {
370
+ LoggerService .log (LogLevelEnum .ERROR , "SETTINGS_SCHEMA_INVALID" , new HashMap <String , String >() {{
371
+ put ("errors" , "Settings object is null" );
372
+ put ("accountId" , options .getAccountId ().toString ());
373
+ put ("sdkKey" , options .getSdkKey ());
374
+ put ("settings" , "null" );
375
+ }});
376
+ return false ;
377
+ }
378
+ SettingsSchema validationResult = new SettingsSchema ().validateSettings (processedSettings );
379
+ if (!validationResult .isValid ()) {
380
+ LoggerService .log (LogLevelEnum .ERROR , "SETTINGS_SCHEMA_INVALID" , new HashMap <String , String >() {{
381
+ put ("errors" , validationResult .getErrorsAsString ());
382
+ put ("accountId" , options .getAccountId ().toString ());
383
+ put ("sdkKey" , options .getSdkKey ());
384
+ put ("settings" , settings );
385
+ }});
386
+ return false ;
387
+ }
388
+ return true ;
389
+ } catch (Exception exception ) {
390
+ LoggerService .log (LogLevelEnum .ERROR , "SETTINGS_SCHEMA_INVALID" , new HashMap <String , String >() {{
391
+ put ("errors" , exception .getMessage ());
392
+ put ("accountId" , options .getAccountId ().toString ());
393
+ put ("sdkKey" , options .getSdkKey ());
394
+ put ("settings" , settings );
395
+ }});
396
+ return false ;
397
+ }
398
+ }
399
+ }
0 commit comments