3030import com .vwo .services .LoggerService ;
3131import com .vwo .services .UrlService ;
3232import com .vwo .utils .DataTypeUtil ;
33- import com .vwo .utils .SDKMetaUtil ;
3433import com .vwo .utils .SettingsUtil ;
3534import com .vwo .services .BatchEventQueue ;
3635import com .vwo .services .SettingsManager ;
@@ -60,8 +59,6 @@ public VWOClient(String settings, VWOInitOptions options) {
6059 SettingsUtil .processSettings (this .processedSettings );
6160 // init url version with collection prefix
6261 UrlService .init (this .processedSettings .getCollectionPrefix ());
63- // init SDKMetaUtil and set sdkVersion
64- SDKMetaUtil .init ();
6562 LoggerService .log (LogLevelEnum .INFO , "CLIENT_INITIALIZED" , null );
6663 } catch (Exception exception ) {
6764 LoggerService .log (LogLevelEnum .ERROR , "exception occurred while parsing settings " + exception .getMessage ());
@@ -101,8 +98,7 @@ public GetFlag getFlag(String featureKey, VWOContext context) {
10198 throw new IllegalArgumentException ("Feature Key is required" );
10299 }
103100
104- if (this .processedSettings == null || !new SettingsSchema ().isSettingsValid (this .processedSettings )) {
105- LoggerService .log (LogLevelEnum .ERROR , "SETTINGS_SCHEMA_INVALID" , null );
101+ if (!validateSettings (this .processedSettings )) {
106102 getFlag .setIsEnabled (false );
107103 return getFlag ;
108104 }
@@ -111,7 +107,7 @@ public GetFlag getFlag(String featureKey, VWOContext context) {
111107 } catch (Exception exception ) {
112108 LoggerService .log (LogLevelEnum .ERROR , "API_THROW_ERROR" , new HashMap <String , String >() {{
113109 put ("apiName" , "getFlag" );
114- put ("err" , exception .toString ());
110+ put ("err" , exception .getMessage ());
115111 }});
116112 getFlag .setIsEnabled (false );
117113 return getFlag ;
@@ -147,8 +143,7 @@ private Map<String, Boolean> track(String eventName, VWOContext context, Map<Str
147143 throw new IllegalArgumentException ("User ID is required" );
148144 }
149145
150- if (this .processedSettings == null || !new SettingsSchema ().isSettingsValid (this .processedSettings )) {
151- LoggerService .log (LogLevelEnum .ERROR , "SETTINGS_SCHEMA_INVALID" , null );
146+ if (!validateSettings (this .processedSettings )) {
152147 resultMap .put (eventName , false );
153148 return resultMap ;
154149 }
@@ -163,7 +158,7 @@ private Map<String, Boolean> track(String eventName, VWOContext context, Map<Str
163158 } catch (Exception exception ) {
164159 LoggerService .log (LogLevelEnum .ERROR , "API_THROW_ERROR" , new HashMap <String , String >() {{
165160 put ("apiName" , apiName );
166- put ("err" , exception .toString ());
161+ put ("err" , exception .getMessage ());
167162 }});
168163 resultMap .put (eventName , false );
169164 return resultMap ;
@@ -228,16 +223,15 @@ public void setAttribute(Map<String, Object> attributeMap, VWOContext context) {
228223 throw new IllegalArgumentException ("User ID is required" );
229224 }
230225
231- if (this .processedSettings == null || !new SettingsSchema ().isSettingsValid (this .processedSettings )) {
232- LoggerService .log (LogLevelEnum .ERROR , "SETTINGS_SCHEMA_INVALID" , null );
226+ if (!validateSettings (this .processedSettings )) {
233227 return ;
234228 }
235229
236230 SetAttributeAPI .setAttribute (this .processedSettings , attributeMap , context );
237231 } catch (Exception exception ) {
238232 LoggerService .log (LogLevelEnum .ERROR , "API_THROW_ERROR" , new HashMap <String , String >() {{
239233 put ("apiName" , apiName );
240- put ("err" , exception .toString ());
234+ put ("err" , exception .getMessage ());
241235 }});
242236 }
243237 }
@@ -287,15 +281,16 @@ private void updateSettingsOnVWOClient(String newSettings) {
287281 throw new IllegalArgumentException ("Settings cannot be empty" );
288282 }
289283 // 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 );
292285 // 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 ;
295290 // Process the new settings and update the client instance
296291 SettingsUtil .processSettings (this .processedSettings );
297292 } else {
298- throw new IllegalStateException ("Settings schema is invalid" );
293+ throw new IllegalStateException ("Settings schema is invalid: " + validationResult . getErrorsAsString () );
299294 }
300295 } catch (Exception exception ) {
301296 throw new IllegalStateException (exception .getMessage ());
@@ -314,6 +309,7 @@ public String updateSettings() {
314309 * @param settings New settings to be updated
315310 */
316311 public String updateSettings (String settings ) {
312+ Boolean isViaWebhook = false ;
317313 String apiName = "updateSettings" ;
318314 try {
319315 LoggerService .log (LogLevelEnum .DEBUG , "API_CALLED" , new HashMap <String , String >() {{
@@ -322,7 +318,8 @@ public String updateSettings(String settings) {
322318
323319 String settingsToUpdate = settings ;
324320 if (settings == null || settings .isEmpty ()) {
325- settingsToUpdate = this .updateSettings (true );
321+ isViaWebhook = true ;
322+ settingsToUpdate = this .updateSettings (isViaWebhook );
326323 }
327324 // Update the settings on the VWOClient instance
328325 this .updateSettingsOnVWOClient (settingsToUpdate );
@@ -331,9 +328,11 @@ public String updateSettings(String settings) {
331328 }});
332329 return settingsToUpdate ;
333330 } catch (Exception exception ) {
331+ Boolean finalIsViaWebhook = isViaWebhook ;
334332 LoggerService .log (LogLevelEnum .ERROR , "SETTINGS_FETCH_FAILED" , new HashMap <String , String >() {{
335333 put ("apiName" , apiName );
336- put ("err" , exception .toString ());
334+ put ("err" , exception .getMessage ());
335+ put ("isViaWebhook" , finalIsViaWebhook .toString ());
337336 }});
338337 return null ;
339338 }
@@ -353,9 +352,48 @@ public String updateSettings(Boolean isViaWebhook) {
353352 LoggerService .log (LogLevelEnum .ERROR , "SETTINGS_FETCH_FAILED" , new HashMap <String , String >() {{
354353 put ("apiName" , apiName );
355354 put ("isViaWebhook" , isViaWebhook .toString ());
356- put ("err" , exception .toString ());
355+ put ("err" , exception .getMessage ());
357356 }});
358357 return null ;
359358 }
360359 }
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