Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FSSDK-11035] refactor thrown exceptions to use OptimizelyException #986

Merged
merged 4 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/common_exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@
* limitations under the License.
*/

export { LOG_LEVEL } from './utils/enums';
export { createStaticProjectConfigManager } from './project_config/config_manager_factory';
export { PollingConfigManagerConfig } from './project_config/config_manager_factory';
1 change: 0 additions & 1 deletion lib/core/audience_evaluator/index.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import AudienceEvaluator, { createAudienceEvaluator } from './index';
import * as conditionTreeEvaluator from '../condition_tree_evaluator';
import * as customAttributeConditionEvaluator from '../custom_attribute_condition_evaluator';
import { AUDIENCE_EVALUATION_RESULT, EVALUATING_AUDIENCE } from '../../log_messages';
// import { getEvaluator } from '../custom_attribute_condition_evaluator';

var buildLogMessageFromArgs = args => sprintf(args[1], ...args.splice(2));
var mockLogger = {
Expand Down
5 changes: 0 additions & 5 deletions lib/core/audience_evaluator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
LOG_LEVEL,
} from '../../utils/enums';
import * as conditionTreeEvaluator from '../condition_tree_evaluator';
import * as customAttributeConditionEvaluator from '../custom_attribute_condition_evaluator';
import * as odpSegmentsConditionEvaluator from './odp_segment_condition_evaluator';
Expand All @@ -24,8 +21,6 @@
import { AUDIENCE_EVALUATION_RESULT, EVALUATING_AUDIENCE} from '../../log_messages';
import { LoggerFacade } from '../../logging/logger';

const MODULE_NAME = 'AUDIENCE_EVALUATOR';

export class AudienceEvaluator {
private logger?: LoggerFacade;

Expand All @@ -45,7 +40,7 @@
constructor(UNSTABLE_conditionEvaluators: unknown, logger?: LoggerFacade) {
this.logger = logger;
this.typeToEvaluatorMap = {
...UNSTABLE_conditionEvaluators as any,

Check warning on line 43 in lib/core/audience_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 43 in lib/core/audience_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (22)

Unexpected any. Specify a different type

Check warning on line 43 in lib/core/audience_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (20)

Unexpected any. Specify a different type

Check warning on line 43 in lib/core/audience_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (18)

Unexpected any. Specify a different type

Check warning on line 43 in lib/core/audience_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (16)

Unexpected any. Specify a different type
custom_attribute: customAttributeConditionEvaluator.getEvaluator(this.logger),
third_party_dimension: odpSegmentsConditionEvaluator.getEvaluator(this.logger),
};
Expand Down Expand Up @@ -109,7 +104,7 @@
}
try {
return evaluator.evaluate(condition, user);
} catch (err: any) {

Check warning on line 107 in lib/core/audience_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 107 in lib/core/audience_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (22)

Unexpected any. Specify a different type

Check warning on line 107 in lib/core/audience_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (20)

Unexpected any. Specify a different type

Check warning on line 107 in lib/core/audience_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (18)

Unexpected any. Specify a different type

Check warning on line 107 in lib/core/audience_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (16)

Unexpected any. Specify a different type
this.logger?.error(
CONDITION_EVALUATOR_ERROR, condition.type, err.message
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import { UNKNOWN_MATCH_TYPE } from '../../../error_messages';
import { LoggerFacade } from '../../../logging/logger';
import { Condition, OptimizelyUserContext } from '../../../shared_types';

const MODULE_NAME = 'ODP_SEGMENT_CONDITION_EVALUATOR';

const QUALIFIED_MATCH_TYPE = 'qualified';

const MATCH_TYPES = [
Expand Down
12 changes: 6 additions & 6 deletions lib/core/bucketer/index.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
USER_NOT_IN_ANY_EXPERIMENT,
USER_ASSIGNED_TO_EXPERIMENT_BUCKET,
} from '.';
import { OptimizelyError } from '../../error/optimizly_error';

var buildLogMessageFromArgs = args => sprintf(args[1], ...args.splice(2));
var testData = getTestProjectConfig();
Expand Down Expand Up @@ -204,9 +205,11 @@ describe('lib/core/bucketer', function () {
var bucketerParamsWithInvalidGroupId = cloneDeep(bucketerParams);
bucketerParamsWithInvalidGroupId.experimentIdMap[configObj.experiments[4].id].groupId = '6969';

assert.throws(function () {
const ex = assert.throws(function () {
bucketer.bucket(bucketerParamsWithInvalidGroupId);
}, sprintf(INVALID_GROUP_ID, 'BUCKETER', '6969'));
});
assert.equal(ex.baseMessage, INVALID_GROUP_ID);
assert.deepEqual(ex.params, ['6969']);
});
});

Expand Down Expand Up @@ -343,10 +346,7 @@ describe('lib/core/bucketer', function () {
const response = assert.throws(function() {
bucketer._generateBucketValue(null);
} );
expect([
sprintf(INVALID_BUCKETING_ID, 'BUCKETER', null, "Cannot read property 'length' of null"), // node v14
sprintf(INVALID_BUCKETING_ID, 'BUCKETER', null, "Cannot read properties of null (reading \'length\')") // node v16
]).contain(response.message);
expect(response.baseMessage).to.equal(INVALID_BUCKETING_ID);
});
});

Expand Down
16 changes: 5 additions & 11 deletions lib/core/bucketer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
/**
* Bucketer API for determining the variation id from the specified parameters
*/
import { sprintf } from '../../utils/fns';
import murmurhash from 'murmurhash';
import { LoggerFacade } from '../../logging/logger';
import {
Expand All @@ -27,8 +26,8 @@
Group,
} from '../../shared_types';

import { LOG_LEVEL } from '../../utils/enums';
import { INVALID_BUCKETING_ID, INVALID_GROUP_ID } from '../../error_messages';
import { OptimizelyError } from '../../error/optimizly_error';

export const USER_NOT_IN_ANY_EXPERIMENT = 'User %s is not in any experiment of group %s.';
export const USER_NOT_BUCKETED_INTO_EXPERIMENT_IN_GROUP = 'User %s is not in experiment %s of group %s.';
Expand All @@ -39,7 +38,6 @@
const HASH_SEED = 1;
const MAX_HASH_VALUE = Math.pow(2, 32);
const MAX_TRAFFIC_VALUE = 10000;
const MODULE_NAME = 'BUCKETER';
const RANDOM_POLICY = 'random';

/**
Expand All @@ -66,7 +64,7 @@
if (groupId) {
const group = bucketerParams.groupIdMap[groupId];
if (!group) {
throw new Error(sprintf(INVALID_GROUP_ID, MODULE_NAME, groupId));
throw new OptimizelyError(INVALID_GROUP_ID, groupId);
}
if (group.policy === RANDOM_POLICY) {
const bucketedExperimentId = bucketUserIntoExperiment(
Expand All @@ -85,7 +83,6 @@
);
decideReasons.push([
USER_NOT_IN_ANY_EXPERIMENT,
MODULE_NAME,
bucketerParams.userId,
groupId,
]);
Expand All @@ -105,7 +102,6 @@
);
decideReasons.push([
USER_NOT_BUCKETED_INTO_EXPERIMENT_IN_GROUP,
MODULE_NAME,
bucketerParams.userId,
bucketerParams.experimentKey,
groupId,
Expand All @@ -125,7 +121,6 @@
);
decideReasons.push([
USER_BUCKETED_INTO_EXPERIMENT_IN_GROUP,
MODULE_NAME,
bucketerParams.userId,
bucketerParams.experimentKey,
groupId,
Expand All @@ -142,7 +137,6 @@
);
decideReasons.push([
USER_ASSIGNED_TO_EXPERIMENT_BUCKET,
MODULE_NAME,
bucketValue,
bucketerParams.userId,
]);
Expand All @@ -151,8 +145,8 @@
if (entityId !== null) {
if (!bucketerParams.variationIdMap[entityId]) {
if (entityId) {
bucketerParams.logger?.warn(INVALID_VARIATION_ID, MODULE_NAME);
decideReasons.push([INVALID_VARIATION_ID, MODULE_NAME]);
bucketerParams.logger?.warn(INVALID_VARIATION_ID);
decideReasons.push([INVALID_VARIATION_ID]);
}
return {
result: null,
Expand Down Expand Up @@ -227,8 +221,8 @@
const hashValue = murmurhash.v3(bucketingKey, HASH_SEED);
const ratio = hashValue / MAX_HASH_VALUE;
return Math.floor(ratio * MAX_TRAFFIC_VALUE);
} catch (ex: any) {

Check warning on line 224 in lib/core/bucketer/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 224 in lib/core/bucketer/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (22)

Unexpected any. Specify a different type

Check warning on line 224 in lib/core/bucketer/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (20)

Unexpected any. Specify a different type

Check warning on line 224 in lib/core/bucketer/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (18)

Unexpected any. Specify a different type

Check warning on line 224 in lib/core/bucketer/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (16)

Unexpected any. Specify a different type
throw new Error(sprintf(INVALID_BUCKETING_ID, MODULE_NAME, bucketingKey, ex.message));
throw new OptimizelyError(INVALID_BUCKETING_ID, bucketingKey, ex.message);
}
};

Expand Down
2 changes: 0 additions & 2 deletions lib/core/custom_attribute_condition_evaluator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
} from '../../error_messages';
import { LoggerFacade } from '../../logging/logger';

const MODULE_NAME = 'CUSTOM_ATTRIBUTE_CONDITION_EVALUATOR';

const EXACT_MATCH_TYPE = 'exact';
const EXISTS_MATCH_TYPE = 'exists';
const GREATER_OR_EQUAL_THAN_MATCH_TYPE = 'ge';
Expand Down Expand Up @@ -190,7 +188,7 @@
* 2) the user attribute value is neither null nor undefined
* Returns false otherwise
*/
function existsEvaluator(condition: Condition, user: OptimizelyUserContext, logger?: LoggerFacade): boolean {

Check warning on line 191 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / lint

'logger' is defined but never used

Check warning on line 191 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (22)

'logger' is defined but never used

Check warning on line 191 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (20)

'logger' is defined but never used

Check warning on line 191 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (18)

'logger' is defined but never used

Check warning on line 191 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (16)

'logger' is defined but never used
const userAttributes = user.getAttributes();
const userValue = userAttributes[condition.name];
return typeof userValue !== 'undefined' && userValue !== null;
Expand Down Expand Up @@ -257,7 +255,7 @@
if (!validateValuesForNumericCondition(condition, user, logger) || conditionValue === null) {
return null;
}
return userValue! > conditionValue;

Check warning on line 258 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion

Check warning on line 258 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (22)

Forbidden non-null assertion

Check warning on line 258 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (20)

Forbidden non-null assertion

Check warning on line 258 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (18)

Forbidden non-null assertion

Check warning on line 258 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (16)

Forbidden non-null assertion
}

/**
Expand All @@ -278,7 +276,7 @@
return null;
}

return userValue! >= conditionValue;

Check warning on line 279 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion

Check warning on line 279 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (22)

Forbidden non-null assertion

Check warning on line 279 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (20)

Forbidden non-null assertion

Check warning on line 279 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (18)

Forbidden non-null assertion

Check warning on line 279 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (16)

Forbidden non-null assertion
}

/**
Expand All @@ -299,7 +297,7 @@
return null;
}

return userValue! < conditionValue;

Check warning on line 300 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion

Check warning on line 300 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (22)

Forbidden non-null assertion

Check warning on line 300 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (20)

Forbidden non-null assertion

Check warning on line 300 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (18)

Forbidden non-null assertion

Check warning on line 300 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (16)

Forbidden non-null assertion
}

/**
Expand All @@ -320,7 +318,7 @@
return null;
}

return userValue! <= conditionValue;

Check warning on line 321 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion

Check warning on line 321 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (22)

Forbidden non-null assertion

Check warning on line 321 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (20)

Forbidden non-null assertion

Check warning on line 321 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (18)

Forbidden non-null assertion

Check warning on line 321 in lib/core/custom_attribute_condition_evaluator/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (16)

Forbidden non-null assertion
}

/**
Expand Down
32 changes: 18 additions & 14 deletions lib/core/decision_service/index.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,31 @@ import {
getTestProjectConfig,
getTestProjectConfigWithFeatures,
} from '../../tests/test_data';

import {
AUDIENCE_EVALUATION_RESULT_COMBINED,
EVALUATING_AUDIENCES_COMBINED,
USER_FORCED_IN_VARIATION,
USER_HAS_NO_FORCED_VARIATION,
USER_DOESNT_MEET_CONDITIONS_FOR_TARGETING_RULE,
USER_NOT_IN_EXPERIMENT,
VALID_BUCKETING_ID,
SAVED_USER_VARIATION,
SAVED_VARIATION_NOT_FOUND,
} from '../../log_messages';

import {
EXPERIMENT_NOT_RUNNING,
RETURNING_STORED_VARIATION,
USER_NOT_IN_EXPERIMENT,
USER_FORCED_IN_VARIATION,
EVALUATING_AUDIENCES_COMBINED,
AUDIENCE_EVALUATION_RESULT_COMBINED,
USER_IN_ROLLOUT,
USER_NOT_IN_ROLLOUT,
FEATURE_HAS_NO_EXPERIMENTS,
NO_ROLLOUT_EXISTS,
USER_DOESNT_MEET_CONDITIONS_FOR_TARGETING_RULE,
USER_NOT_BUCKETED_INTO_TARGETING_RULE,
USER_BUCKETED_INTO_TARGETING_RULE,
USER_IN_ROLLOUT,
NO_ROLLOUT_EXISTS,
USER_MEETS_CONDITIONS_FOR_TARGETING_RULE,
USER_NOT_BUCKETED_INTO_TARGETING_RULE,
USER_NOT_IN_ROLLOUT,
VALID_BUCKETING_ID,
SAVED_USER_VARIATION,
SAVED_VARIATION_NOT_FOUND
} from '../../log_messages';
import { mock } from 'node:test';
} from '../decision_service/index';

import { BUCKETING_ID_NOT_STRING, USER_PROFILE_LOOKUP_ERROR, USER_PROFILE_SAVE_ERROR } from '../../error_messages';

var testData = getTestProjectConfig();
Expand Down
Loading
Loading