Skip to content

Commit 51d882f

Browse files
committed
[FSSDK-11101] refactor handling of no config availability
1 parent 25e81e2 commit 51d882f

File tree

2 files changed

+43
-66
lines changed

2 files changed

+43
-66
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: 42 additions & 65 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,
@@ -267,11 +268,9 @@ export default class Optimizely implements Client {
267268

268269
/**
269270
* 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.
272271
* @return {boolean}
273272
*/
274-
isValidInstance(): boolean {
273+
private hasProjectConfig(): boolean {
275274
return !!this.projectConfigManager.getConfig();
276275
}
277276

@@ -284,20 +283,16 @@ export default class Optimizely implements Client {
284283
*/
285284
activate(experimentKey: string, userId: string, attributes?: UserAttributes): string | null {
286285
try {
287-
if (!this.isValidInstance()) {
288-
this.logger?.error(INVALID_OBJECT, 'activate');
286+
const configObj = this.projectConfigManager.getConfig();
287+
if (!configObj) {
288+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'activate');
289289
return null;
290290
}
291291

292292
if (!this.validateInputs({ experiment_key: experimentKey, user_id: userId }, attributes)) {
293293
return this.notActivatingExperiment(experimentKey, userId);
294294
}
295295

296-
const configObj = this.projectConfigManager.getConfig();
297-
if (!configObj) {
298-
return null;
299-
}
300-
301296
try {
302297
const variationKey = this.getVariation(experimentKey, userId, attributes);
303298
if (variationKey === null) {
@@ -394,20 +389,16 @@ export default class Optimizely implements Client {
394389
return;
395390
}
396391

397-
if (!this.isValidInstance()) {
398-
this.logger?.error(INVALID_OBJECT, 'track');
392+
const configObj = this.projectConfigManager.getConfig();
393+
if (!configObj) {
394+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'track');
399395
return;
400396
}
401397

402398
if (!this.validateInputs({ user_id: userId, event_key: eventKey }, attributes, eventTags)) {
403399
return;
404400
}
405401

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

412403
if (!projectConfig.eventWithKeyExists(configObj, eventKey)) {
413404
this.logger?.warn(EVENT_KEY_NOT_FOUND, eventKey);
@@ -453,8 +444,9 @@ export default class Optimizely implements Client {
453444
*/
454445
getVariation(experimentKey: string, userId: string, attributes?: UserAttributes): string | null {
455446
try {
456-
if (!this.isValidInstance()) {
457-
this.logger?.error(INVALID_OBJECT, 'getVariation');
447+
const configObj = this.projectConfigManager.getConfig();
448+
if (!configObj) {
449+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getVariation');
458450
return null;
459451
}
460452

@@ -463,11 +455,6 @@ export default class Optimizely implements Client {
463455
return null;
464456
}
465457

466-
const configObj = this.projectConfigManager.getConfig();
467-
if (!configObj) {
468-
return null;
469-
}
470-
471458
const experiment = configObj.experimentKeyMap[experimentKey];
472459
if (!experiment || experiment.isRollout) {
473460
this.logger?.debug(INVALID_EXPERIMENT_KEY_INFO, experimentKey);
@@ -624,20 +611,16 @@ export default class Optimizely implements Client {
624611
*/
625612
isFeatureEnabled(featureKey: string, userId: string, attributes?: UserAttributes): boolean {
626613
try {
627-
if (!this.isValidInstance()) {
628-
this.logger?.error(INVALID_OBJECT, 'isFeatureEnabled');
614+
const configObj = this.projectConfigManager.getConfig();
615+
if (!configObj) {
616+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'isFeatureEnabled');
629617
return false;
630618
}
631619

632620
if (!this.validateInputs({ feature_key: featureKey, user_id: userId }, attributes)) {
633621
return false;
634622
}
635623

636-
const configObj = this.projectConfigManager.getConfig();
637-
if (!configObj) {
638-
return false;
639-
}
640-
641624
const feature = projectConfig.getFeatureFromKey(configObj, featureKey, this.logger);
642625
if (!feature) {
643626
return false;
@@ -704,17 +687,14 @@ export default class Optimizely implements Client {
704687
getEnabledFeatures(userId: string, attributes?: UserAttributes): string[] {
705688
try {
706689
const enabledFeatures: string[] = [];
707-
if (!this.isValidInstance()) {
708-
this.logger?.error(INVALID_OBJECT, 'getEnabledFeatures');
709-
return enabledFeatures;
710-
}
711690

712-
if (!this.validateInputs({ user_id: userId })) {
691+
const configObj = this.projectConfigManager.getConfig();
692+
if (!configObj) {
693+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getEnabledFeatures');
713694
return enabledFeatures;
714695
}
715696

716-
const configObj = this.projectConfigManager.getConfig();
717-
if (!configObj) {
697+
if (!this.validateInputs({ user_id: userId })) {
718698
return enabledFeatures;
719699
}
720700

@@ -752,8 +732,8 @@ export default class Optimizely implements Client {
752732
attributes?: UserAttributes
753733
): FeatureVariableValue {
754734
try {
755-
if (!this.isValidInstance()) {
756-
this.logger?.error(INVALID_OBJECT, 'getFeatureVariable');
735+
if (!this.hasProjectConfig()) {
736+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getFeatureVariable');
757737
return null;
758738
}
759739
return this.getFeatureVariableForType(featureKey, variableKey, null, userId, attributes);
@@ -946,8 +926,8 @@ export default class Optimizely implements Client {
946926
attributes?: UserAttributes
947927
): boolean | null {
948928
try {
949-
if (!this.isValidInstance()) {
950-
this.logger?.error(INVALID_OBJECT, 'getFeatureVariableBoolean');
929+
if (!this.hasProjectConfig()) {
930+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getFeatureVariableBoolean');
951931
return null;
952932
}
953933
return this.getFeatureVariableForType(
@@ -984,8 +964,8 @@ export default class Optimizely implements Client {
984964
attributes?: UserAttributes
985965
): number | null {
986966
try {
987-
if (!this.isValidInstance()) {
988-
this.logger?.error(INVALID_OBJECT, 'getFeatureVariableDouble');
967+
if (!this.hasProjectConfig()) {
968+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getFeatureVariableDouble');
989969
return null;
990970
}
991971
return this.getFeatureVariableForType(
@@ -1022,8 +1002,8 @@ export default class Optimizely implements Client {
10221002
attributes?: UserAttributes
10231003
): number | null {
10241004
try {
1025-
if (!this.isValidInstance()) {
1026-
this.logger?.error(INVALID_OBJECT, 'getFeatureVariableInteger');
1005+
if (!this.hasProjectConfig()) {
1006+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getFeatureVariableInteger');
10271007
return null;
10281008
}
10291009
return this.getFeatureVariableForType(
@@ -1060,8 +1040,8 @@ export default class Optimizely implements Client {
10601040
attributes?: UserAttributes
10611041
): string | null {
10621042
try {
1063-
if (!this.isValidInstance()) {
1064-
this.logger?.error(INVALID_OBJECT, 'getFeatureVariableString');
1043+
if (!this.hasProjectConfig()) {
1044+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getFeatureVariableString');
10651045
return null;
10661046
}
10671047
return this.getFeatureVariableForType(
@@ -1093,8 +1073,8 @@ export default class Optimizely implements Client {
10931073
*/
10941074
getFeatureVariableJSON(featureKey: string, variableKey: string, userId: string, attributes: UserAttributes): unknown {
10951075
try {
1096-
if (!this.isValidInstance()) {
1097-
this.logger?.error(INVALID_OBJECT, 'getFeatureVariableJSON');
1076+
if (!this.hasProjectConfig()) {
1077+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getFeatureVariableJSON');
10981078
return null;
10991079
}
11001080
return this.getFeatureVariableForType(featureKey, variableKey, FEATURE_VARIABLE_TYPES.JSON, userId, attributes);
@@ -1120,17 +1100,14 @@ export default class Optimizely implements Client {
11201100
attributes?: UserAttributes
11211101
): { [variableKey: string]: unknown } | null {
11221102
try {
1123-
if (!this.isValidInstance()) {
1124-
this.logger?.error(INVALID_OBJECT, 'getAllFeatureVariables');
1125-
return null;
1126-
}
1103+
const configObj = this.projectConfigManager.getConfig();
11271104

1128-
if (!this.validateInputs({ feature_key: featureKey, user_id: userId }, attributes)) {
1105+
if (!configObj) {
1106+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getAllFeatureVariables');
11291107
return null;
11301108
}
11311109

1132-
const configObj = this.projectConfigManager.getConfig();
1133-
if (!configObj) {
1110+
if (!this.validateInputs({ feature_key: featureKey, user_id: userId }, attributes)) {
11341111
return null;
11351112
}
11361113

@@ -1425,8 +1402,8 @@ export default class Optimizely implements Client {
14251402
decide(user: OptimizelyUserContext, key: string, options: OptimizelyDecideOption[] = []): OptimizelyDecision {
14261403
const configObj = this.projectConfigManager.getConfig();
14271404

1428-
if (!this.isValidInstance() || !configObj) {
1429-
this.logger?.error(INVALID_OBJECT, 'decide');
1405+
if (!configObj) {
1406+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'decide');
14301407
return newErrorDecision(key, user, [DECISION_MESSAGES.SDK_NOT_READY]);
14311408
}
14321409

@@ -1570,8 +1547,8 @@ export default class Optimizely implements Client {
15701547

15711548
const configObj = this.projectConfigManager.getConfig()
15721549

1573-
if (!this.isValidInstance() || !configObj) {
1574-
this.logger?.error(INVALID_OBJECT, 'decideForKeys');
1550+
if (!configObj) {
1551+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'decideForKeys');
15751552
return decisionMap;
15761553
}
15771554
if (keys.length === 0) {
@@ -1638,10 +1615,10 @@ export default class Optimizely implements Client {
16381615
user: OptimizelyUserContext,
16391616
options: OptimizelyDecideOption[] = []
16401617
): { [key: string]: OptimizelyDecision } {
1641-
const configObj = this.projectConfigManager.getConfig();
16421618
const decisionMap: { [key: string]: OptimizelyDecision } = {};
1643-
if (!this.isValidInstance() || !configObj) {
1644-
this.logger?.error(INVALID_OBJECT, 'decideAll');
1619+
const configObj = this.projectConfigManager.getConfig();
1620+
if (!configObj) {
1621+
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'decideAll');
16451622
return decisionMap;
16461623
}
16471624

0 commit comments

Comments
 (0)