Skip to content

Commit

Permalink
[FSSDK-11035] refactor thrown exceptions to use OptimizelyException (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
raju-opti authored Jan 21, 2025
1 parent 36d7ef7 commit 3d2523d
Show file tree
Hide file tree
Showing 49 changed files with 343 additions and 413 deletions.
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 { CONDITION_EVALUATOR_ERROR, UNKNOWN_CONDITION_TYPE } from '../../error_m
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 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 @@ import {
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 @@ export const INVALID_VARIATION_ID = 'Bucketed into an invalid variation ID. Retu
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 @@ export const bucket = function(bucketerParams: BucketerParams): DecisionResponse
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 @@ export const bucket = function(bucketerParams: BucketerParams): DecisionResponse
);
decideReasons.push([
USER_NOT_IN_ANY_EXPERIMENT,
MODULE_NAME,
bucketerParams.userId,
groupId,
]);
Expand All @@ -105,7 +102,6 @@ export const bucket = function(bucketerParams: BucketerParams): DecisionResponse
);
decideReasons.push([
USER_NOT_BUCKETED_INTO_EXPERIMENT_IN_GROUP,
MODULE_NAME,
bucketerParams.userId,
bucketerParams.experimentKey,
groupId,
Expand All @@ -125,7 +121,6 @@ export const bucket = function(bucketerParams: BucketerParams): DecisionResponse
);
decideReasons.push([
USER_BUCKETED_INTO_EXPERIMENT_IN_GROUP,
MODULE_NAME,
bucketerParams.userId,
bucketerParams.experimentKey,
groupId,
Expand All @@ -142,7 +137,6 @@ export const bucket = function(bucketerParams: BucketerParams): DecisionResponse
);
decideReasons.push([
USER_ASSIGNED_TO_EXPERIMENT_BUCKET,
MODULE_NAME,
bucketValue,
bucketerParams.userId,
]);
Expand All @@ -151,8 +145,8 @@ export const bucket = function(bucketerParams: BucketerParams): DecisionResponse
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 @@ -228,7 +222,7 @@ export const _generateBucketValue = function(bucketingKey: string): number {
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 (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 (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 (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 @@ import {
} 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
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

0 comments on commit 3d2523d

Please sign in to comment.