Skip to content

Commit bc49e3c

Browse files
authored
[FSSDK-11101] refactor handling of no config availability (#998)
1 parent 25e81e2 commit bc49e3c

File tree

2 files changed

+52
-83
lines changed

2 files changed

+52
-83
lines changed

lib/message/error_message.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export const UNKNOWN_CONDITION_TYPE =
7171
export const UNKNOWN_MATCH_TYPE =
7272
'Audience condition %s uses an unknown match type. You may need to upgrade to a newer release of the Optimizely SDK.';
7373
export const UNRECOGNIZED_DECIDE_OPTION = 'Unrecognized decide option %s provided.';
74-
export const INVALID_OBJECT = 'Optimizely object is not valid. Failing %s.';
74+
export const NO_PROJECT_CONFIG_FAILURE = 'No project config available. Failing %s.';
7575
export const EVENT_KEY_NOT_FOUND = 'Event key %s is not in datafile.';
7676
export const NOT_TRACKING_USER = 'Not tracking user %s.';
7777
export const VARIABLE_REQUESTED_WITH_WRONG_TYPE =

lib/optimizely/index.ts

Lines changed: 51 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { OdpManager } from '../odp/odp_manager';
2222
import { VuidManager } from '../vuid/vuid_manager';
2323
import { OdpEvent } from '../odp/event_manager/odp_event';
2424
import { OptimizelySegmentOption } from '../odp/segment_manager/optimizely_segment_option';
25+
import { BaseService } from '../service';
2526

2627
import {
2728
UserAttributes,
@@ -71,7 +72,7 @@ import {
7172
ODP_EVENT_FAILED_ODP_MANAGER_MISSING,
7273
UNABLE_TO_GET_VUID_VUID_MANAGER_NOT_AVAILABLE,
7374
UNRECOGNIZED_DECIDE_OPTION,
74-
INVALID_OBJECT,
75+
NO_PROJECT_CONFIG_FAILURE,
7576
EVENT_KEY_NOT_FOUND,
7677
NOT_TRACKING_USER,
7778
VARIABLE_REQUESTED_WITH_WRONG_TYPE,
@@ -265,16 +266,6 @@ export default class Optimizely implements Client {
265266
return this.projectConfigManager.getConfig() || null;
266267
}
267268

268-
/**
269-
* Returns a truthy value if this instance currently has a valid project config
270-
* object, and the initial configuration object that was passed into the
271-
* constructor was also valid.
272-
* @return {boolean}
273-
*/
274-
isValidInstance(): boolean {
275-
return !!this.projectConfigManager.getConfig();
276-
}
277-
278269
/**
279270
* Buckets visitor and sends impression event to Optimizely.
280271
* @param {string} experimentKey
@@ -284,20 +275,16 @@ export default class Optimizely implements Client {
284275
*/
285276
activate(experimentKey: string, userId: string, attributes?: UserAttributes): string | null {
286277
try {
287-
if (!this.isValidInstance()) {
288-
this.logger?.error(INVALID_OBJECT, 'activate');
278+
const configObj = this.getProjectConfig();
279+
if (!configObj) {
280+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'activate');
289281
return null;
290282
}
291283

292284
if (!this.validateInputs({ experiment_key: experimentKey, user_id: userId }, attributes)) {
293285
return this.notActivatingExperiment(experimentKey, userId);
294286
}
295287

296-
const configObj = this.projectConfigManager.getConfig();
297-
if (!configObj) {
298-
return null;
299-
}
300-
301288
try {
302289
const variationKey = this.getVariation(experimentKey, userId, attributes);
303290
if (variationKey === null) {
@@ -353,7 +340,7 @@ export default class Optimizely implements Client {
353340
return;
354341
}
355342

356-
const configObj = this.projectConfigManager.getConfig();
343+
const configObj = this.getProjectConfig();
357344
if (!configObj) {
358345
return;
359346
}
@@ -394,20 +381,16 @@ export default class Optimizely implements Client {
394381
return;
395382
}
396383

397-
if (!this.isValidInstance()) {
398-
this.logger?.error(INVALID_OBJECT, 'track');
384+
const configObj = this.getProjectConfig();
385+
if (!configObj) {
386+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'track');
399387
return;
400388
}
401389

402390
if (!this.validateInputs({ user_id: userId, event_key: eventKey }, attributes, eventTags)) {
403391
return;
404392
}
405393

406-
const configObj = this.projectConfigManager.getConfig();
407-
if (!configObj) {
408-
return;
409-
}
410-
411394

412395
if (!projectConfig.eventWithKeyExists(configObj, eventKey)) {
413396
this.logger?.warn(EVENT_KEY_NOT_FOUND, eventKey);
@@ -453,8 +436,9 @@ export default class Optimizely implements Client {
453436
*/
454437
getVariation(experimentKey: string, userId: string, attributes?: UserAttributes): string | null {
455438
try {
456-
if (!this.isValidInstance()) {
457-
this.logger?.error(INVALID_OBJECT, 'getVariation');
439+
const configObj = this.getProjectConfig();
440+
if (!configObj) {
441+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getVariation');
458442
return null;
459443
}
460444

@@ -463,11 +447,6 @@ export default class Optimizely implements Client {
463447
return null;
464448
}
465449

466-
const configObj = this.projectConfigManager.getConfig();
467-
if (!configObj) {
468-
return null;
469-
}
470-
471450
const experiment = configObj.experimentKeyMap[experimentKey];
472451
if (!experiment || experiment.isRollout) {
473452
this.logger?.debug(INVALID_EXPERIMENT_KEY_INFO, experimentKey);
@@ -517,7 +496,7 @@ export default class Optimizely implements Client {
517496
return false;
518497
}
519498

520-
const configObj = this.projectConfigManager.getConfig();
499+
const configObj = this.getProjectConfig();
521500
if (!configObj) {
522501
return false;
523502
}
@@ -541,7 +520,7 @@ export default class Optimizely implements Client {
541520
return null;
542521
}
543522

544-
const configObj = this.projectConfigManager.getConfig();
523+
const configObj = this.getProjectConfig();
545524
if (!configObj) {
546525
return null;
547526
}
@@ -624,20 +603,16 @@ export default class Optimizely implements Client {
624603
*/
625604
isFeatureEnabled(featureKey: string, userId: string, attributes?: UserAttributes): boolean {
626605
try {
627-
if (!this.isValidInstance()) {
628-
this.logger?.error(INVALID_OBJECT, 'isFeatureEnabled');
606+
const configObj = this.getProjectConfig();
607+
if (!configObj) {
608+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'isFeatureEnabled');
629609
return false;
630610
}
631611

632612
if (!this.validateInputs({ feature_key: featureKey, user_id: userId }, attributes)) {
633613
return false;
634614
}
635615

636-
const configObj = this.projectConfigManager.getConfig();
637-
if (!configObj) {
638-
return false;
639-
}
640-
641616
const feature = projectConfig.getFeatureFromKey(configObj, featureKey, this.logger);
642617
if (!feature) {
643618
return false;
@@ -704,17 +679,14 @@ export default class Optimizely implements Client {
704679
getEnabledFeatures(userId: string, attributes?: UserAttributes): string[] {
705680
try {
706681
const enabledFeatures: string[] = [];
707-
if (!this.isValidInstance()) {
708-
this.logger?.error(INVALID_OBJECT, 'getEnabledFeatures');
709-
return enabledFeatures;
710-
}
711682

712-
if (!this.validateInputs({ user_id: userId })) {
683+
const configObj = this.getProjectConfig();
684+
if (!configObj) {
685+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getEnabledFeatures');
713686
return enabledFeatures;
714687
}
715688

716-
const configObj = this.projectConfigManager.getConfig();
717-
if (!configObj) {
689+
if (!this.validateInputs({ user_id: userId })) {
718690
return enabledFeatures;
719691
}
720692

@@ -752,8 +724,8 @@ export default class Optimizely implements Client {
752724
attributes?: UserAttributes
753725
): FeatureVariableValue {
754726
try {
755-
if (!this.isValidInstance()) {
756-
this.logger?.error(INVALID_OBJECT, 'getFeatureVariable');
727+
if (!this.getProjectConfig()) {
728+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getFeatureVariable');
757729
return null;
758730
}
759731
return this.getFeatureVariableForType(featureKey, variableKey, null, userId, attributes);
@@ -796,7 +768,7 @@ export default class Optimizely implements Client {
796768
return null;
797769
}
798770

799-
const configObj = this.projectConfigManager.getConfig();
771+
const configObj = this.getProjectConfig();
800772
if (!configObj) {
801773
return null;
802774
}
@@ -882,7 +854,7 @@ export default class Optimizely implements Client {
882854
variable: FeatureVariable,
883855
userId: string
884856
): FeatureVariableValue {
885-
const configObj = this.projectConfigManager.getConfig();
857+
const configObj = this.getProjectConfig();
886858
if (!configObj) {
887859
return null;
888860
}
@@ -946,8 +918,8 @@ export default class Optimizely implements Client {
946918
attributes?: UserAttributes
947919
): boolean | null {
948920
try {
949-
if (!this.isValidInstance()) {
950-
this.logger?.error(INVALID_OBJECT, 'getFeatureVariableBoolean');
921+
if (!this.getProjectConfig()) {
922+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getFeatureVariableBoolean');
951923
return null;
952924
}
953925
return this.getFeatureVariableForType(
@@ -984,8 +956,8 @@ export default class Optimizely implements Client {
984956
attributes?: UserAttributes
985957
): number | null {
986958
try {
987-
if (!this.isValidInstance()) {
988-
this.logger?.error(INVALID_OBJECT, 'getFeatureVariableDouble');
959+
if (!this.getProjectConfig()) {
960+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getFeatureVariableDouble');
989961
return null;
990962
}
991963
return this.getFeatureVariableForType(
@@ -1022,8 +994,8 @@ export default class Optimizely implements Client {
1022994
attributes?: UserAttributes
1023995
): number | null {
1024996
try {
1025-
if (!this.isValidInstance()) {
1026-
this.logger?.error(INVALID_OBJECT, 'getFeatureVariableInteger');
997+
if (!this.getProjectConfig()) {
998+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getFeatureVariableInteger');
1027999
return null;
10281000
}
10291001
return this.getFeatureVariableForType(
@@ -1060,8 +1032,8 @@ export default class Optimizely implements Client {
10601032
attributes?: UserAttributes
10611033
): string | null {
10621034
try {
1063-
if (!this.isValidInstance()) {
1064-
this.logger?.error(INVALID_OBJECT, 'getFeatureVariableString');
1035+
if (!this.getProjectConfig()) {
1036+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getFeatureVariableString');
10651037
return null;
10661038
}
10671039
return this.getFeatureVariableForType(
@@ -1093,8 +1065,8 @@ export default class Optimizely implements Client {
10931065
*/
10941066
getFeatureVariableJSON(featureKey: string, variableKey: string, userId: string, attributes: UserAttributes): unknown {
10951067
try {
1096-
if (!this.isValidInstance()) {
1097-
this.logger?.error(INVALID_OBJECT, 'getFeatureVariableJSON');
1068+
if (!this.getProjectConfig()) {
1069+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getFeatureVariableJSON');
10981070
return null;
10991071
}
11001072
return this.getFeatureVariableForType(featureKey, variableKey, FEATURE_VARIABLE_TYPES.JSON, userId, attributes);
@@ -1120,17 +1092,14 @@ export default class Optimizely implements Client {
11201092
attributes?: UserAttributes
11211093
): { [variableKey: string]: unknown } | null {
11221094
try {
1123-
if (!this.isValidInstance()) {
1124-
this.logger?.error(INVALID_OBJECT, 'getAllFeatureVariables');
1125-
return null;
1126-
}
1095+
const configObj = this.getProjectConfig();
11271096

1128-
if (!this.validateInputs({ feature_key: featureKey, user_id: userId }, attributes)) {
1097+
if (!configObj) {
1098+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getAllFeatureVariables');
11291099
return null;
11301100
}
11311101

1132-
const configObj = this.projectConfigManager.getConfig();
1133-
if (!configObj) {
1102+
if (!this.validateInputs({ feature_key: featureKey, user_id: userId }, attributes)) {
11341103
return null;
11351104
}
11361105

@@ -1224,7 +1193,7 @@ export default class Optimizely implements Client {
12241193
*/
12251194
getOptimizelyConfig(): OptimizelyConfig | null {
12261195
try {
1227-
const configObj = this.projectConfigManager.getConfig();
1196+
const configObj = this.getProjectConfig();
12281197
if (!configObj) {
12291198
return null;
12301199
}
@@ -1423,10 +1392,10 @@ export default class Optimizely implements Client {
14231392
}
14241393

14251394
decide(user: OptimizelyUserContext, key: string, options: OptimizelyDecideOption[] = []): OptimizelyDecision {
1426-
const configObj = this.projectConfigManager.getConfig();
1395+
const configObj = this.getProjectConfig();
14271396

1428-
if (!this.isValidInstance() || !configObj) {
1429-
this.logger?.error(INVALID_OBJECT, 'decide');
1397+
if (!configObj) {
1398+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'decide');
14301399
return newErrorDecision(key, user, [DECISION_MESSAGES.SDK_NOT_READY]);
14311400
}
14321401

@@ -1568,10 +1537,10 @@ export default class Optimizely implements Client {
15681537
const flagsWithoutForcedDecision = [];
15691538
const validKeys = [];
15701539

1571-
const configObj = this.projectConfigManager.getConfig()
1540+
const configObj = this.getProjectConfig()
15721541

1573-
if (!this.isValidInstance() || !configObj) {
1574-
this.logger?.error(INVALID_OBJECT, 'decideForKeys');
1542+
if (!configObj) {
1543+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'decideForKeys');
15751544
return decisionMap;
15761545
}
15771546
if (keys.length === 0) {
@@ -1638,10 +1607,10 @@ export default class Optimizely implements Client {
16381607
user: OptimizelyUserContext,
16391608
options: OptimizelyDecideOption[] = []
16401609
): { [key: string]: OptimizelyDecision } {
1641-
const configObj = this.projectConfigManager.getConfig();
16421610
const decisionMap: { [key: string]: OptimizelyDecision } = {};
1643-
if (!this.isValidInstance() || !configObj) {
1644-
this.logger?.error(INVALID_OBJECT, 'decideAll');
1611+
const configObj = this.getProjectConfig();
1612+
if (!configObj) {
1613+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'decideAll');
16451614
return decisionMap;
16461615
}
16471616

@@ -1654,7 +1623,7 @@ export default class Optimizely implements Client {
16541623
* Updates ODP Config with most recent ODP key, host, pixelUrl, and segments from the project config
16551624
*/
16561625
private updateOdpSettings(): void {
1657-
const projectConfig = this.projectConfigManager.getConfig();
1626+
const projectConfig = this.getProjectConfig();
16581627

16591628
if (!projectConfig) {
16601629
return;
@@ -1696,7 +1665,7 @@ export default class Optimizely implements Client {
16961665
* @returns { boolean } `true` if ODP settings were found in the datafile otherwise `false`
16971666
*/
16981667
public isOdpIntegrated(): boolean {
1699-
return this.projectConfigManager.getConfig()?.odpIntegrationConfig?.integrated ?? false;
1668+
return this.getProjectConfig()?.odpIntegrationConfig?.integrated ?? false;
17001669
}
17011670

17021671
/**

0 commit comments

Comments
 (0)