Skip to content

Commit 3d2523d

Browse files
authored
[FSSDK-11035] refactor thrown exceptions to use OptimizelyException (#986)
1 parent 36d7ef7 commit 3d2523d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+343
-413
lines changed

lib/common_exports.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,5 @@
1414
* limitations under the License.
1515
*/
1616

17-
export { LOG_LEVEL } from './utils/enums';
1817
export { createStaticProjectConfigManager } from './project_config/config_manager_factory';
1918
export { PollingConfigManagerConfig } from './project_config/config_manager_factory';

lib/core/audience_evaluator/index.tests.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import AudienceEvaluator, { createAudienceEvaluator } from './index';
2121
import * as conditionTreeEvaluator from '../condition_tree_evaluator';
2222
import * as customAttributeConditionEvaluator from '../custom_attribute_condition_evaluator';
2323
import { AUDIENCE_EVALUATION_RESULT, EVALUATING_AUDIENCE } from '../../log_messages';
24-
// import { getEvaluator } from '../custom_attribute_condition_evaluator';
2524

2625
var buildLogMessageFromArgs = args => sprintf(args[1], ...args.splice(2));
2726
var mockLogger = {

lib/core/audience_evaluator/index.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import {
17-
LOG_LEVEL,
18-
} from '../../utils/enums';
1916
import * as conditionTreeEvaluator from '../condition_tree_evaluator';
2017
import * as customAttributeConditionEvaluator from '../custom_attribute_condition_evaluator';
2118
import * as odpSegmentsConditionEvaluator from './odp_segment_condition_evaluator';
@@ -24,8 +21,6 @@ import { CONDITION_EVALUATOR_ERROR, UNKNOWN_CONDITION_TYPE } from '../../error_m
2421
import { AUDIENCE_EVALUATION_RESULT, EVALUATING_AUDIENCE} from '../../log_messages';
2522
import { LoggerFacade } from '../../logging/logger';
2623

27-
const MODULE_NAME = 'AUDIENCE_EVALUATOR';
28-
2924
export class AudienceEvaluator {
3025
private logger?: LoggerFacade;
3126

lib/core/audience_evaluator/odp_segment_condition_evaluator/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import { UNKNOWN_MATCH_TYPE } from '../../../error_messages';
1717
import { LoggerFacade } from '../../../logging/logger';
1818
import { Condition, OptimizelyUserContext } from '../../../shared_types';
1919

20-
const MODULE_NAME = 'ODP_SEGMENT_CONDITION_EVALUATOR';
21-
2220
const QUALIFIED_MATCH_TYPE = 'qualified';
2321

2422
const MATCH_TYPES = [

lib/core/bucketer/index.tests.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
USER_NOT_IN_ANY_EXPERIMENT,
3030
USER_ASSIGNED_TO_EXPERIMENT_BUCKET,
3131
} from '.';
32+
import { OptimizelyError } from '../../error/optimizly_error';
3233

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

207-
assert.throws(function () {
208+
const ex = assert.throws(function () {
208209
bucketer.bucket(bucketerParamsWithInvalidGroupId);
209-
}, sprintf(INVALID_GROUP_ID, 'BUCKETER', '6969'));
210+
});
211+
assert.equal(ex.baseMessage, INVALID_GROUP_ID);
212+
assert.deepEqual(ex.params, ['6969']);
210213
});
211214
});
212215

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

lib/core/bucketer/index.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
/**
1818
* Bucketer API for determining the variation id from the specified parameters
1919
*/
20-
import { sprintf } from '../../utils/fns';
2120
import murmurhash from 'murmurhash';
2221
import { LoggerFacade } from '../../logging/logger';
2322
import {
@@ -27,8 +26,8 @@ import {
2726
Group,
2827
} from '../../shared_types';
2928

30-
import { LOG_LEVEL } from '../../utils/enums';
3129
import { INVALID_BUCKETING_ID, INVALID_GROUP_ID } from '../../error_messages';
30+
import { OptimizelyError } from '../../error/optimizly_error';
3231

3332
export const USER_NOT_IN_ANY_EXPERIMENT = 'User %s is not in any experiment of group %s.';
3433
export const USER_NOT_BUCKETED_INTO_EXPERIMENT_IN_GROUP = 'User %s is not in experiment %s of group %s.';
@@ -39,7 +38,6 @@ export const INVALID_VARIATION_ID = 'Bucketed into an invalid variation ID. Retu
3938
const HASH_SEED = 1;
4039
const MAX_HASH_VALUE = Math.pow(2, 32);
4140
const MAX_TRAFFIC_VALUE = 10000;
42-
const MODULE_NAME = 'BUCKETER';
4341
const RANDOM_POLICY = 'random';
4442

4543
/**
@@ -66,7 +64,7 @@ export const bucket = function(bucketerParams: BucketerParams): DecisionResponse
6664
if (groupId) {
6765
const group = bucketerParams.groupIdMap[groupId];
6866
if (!group) {
69-
throw new Error(sprintf(INVALID_GROUP_ID, MODULE_NAME, groupId));
67+
throw new OptimizelyError(INVALID_GROUP_ID, groupId);
7068
}
7169
if (group.policy === RANDOM_POLICY) {
7270
const bucketedExperimentId = bucketUserIntoExperiment(
@@ -85,7 +83,6 @@ export const bucket = function(bucketerParams: BucketerParams): DecisionResponse
8583
);
8684
decideReasons.push([
8785
USER_NOT_IN_ANY_EXPERIMENT,
88-
MODULE_NAME,
8986
bucketerParams.userId,
9087
groupId,
9188
]);
@@ -105,7 +102,6 @@ export const bucket = function(bucketerParams: BucketerParams): DecisionResponse
105102
);
106103
decideReasons.push([
107104
USER_NOT_BUCKETED_INTO_EXPERIMENT_IN_GROUP,
108-
MODULE_NAME,
109105
bucketerParams.userId,
110106
bucketerParams.experimentKey,
111107
groupId,
@@ -125,7 +121,6 @@ export const bucket = function(bucketerParams: BucketerParams): DecisionResponse
125121
);
126122
decideReasons.push([
127123
USER_BUCKETED_INTO_EXPERIMENT_IN_GROUP,
128-
MODULE_NAME,
129124
bucketerParams.userId,
130125
bucketerParams.experimentKey,
131126
groupId,
@@ -142,7 +137,6 @@ export const bucket = function(bucketerParams: BucketerParams): DecisionResponse
142137
);
143138
decideReasons.push([
144139
USER_ASSIGNED_TO_EXPERIMENT_BUCKET,
145-
MODULE_NAME,
146140
bucketValue,
147141
bucketerParams.userId,
148142
]);
@@ -151,8 +145,8 @@ export const bucket = function(bucketerParams: BucketerParams): DecisionResponse
151145
if (entityId !== null) {
152146
if (!bucketerParams.variationIdMap[entityId]) {
153147
if (entityId) {
154-
bucketerParams.logger?.warn(INVALID_VARIATION_ID, MODULE_NAME);
155-
decideReasons.push([INVALID_VARIATION_ID, MODULE_NAME]);
148+
bucketerParams.logger?.warn(INVALID_VARIATION_ID);
149+
decideReasons.push([INVALID_VARIATION_ID]);
156150
}
157151
return {
158152
result: null,
@@ -228,7 +222,7 @@ export const _generateBucketValue = function(bucketingKey: string): number {
228222
const ratio = hashValue / MAX_HASH_VALUE;
229223
return Math.floor(ratio * MAX_TRAFFIC_VALUE);
230224
} catch (ex: any) {
231-
throw new Error(sprintf(INVALID_BUCKETING_ID, MODULE_NAME, bucketingKey, ex.message));
225+
throw new OptimizelyError(INVALID_BUCKETING_ID, bucketingKey, ex.message);
232226
}
233227
};
234228

lib/core/custom_attribute_condition_evaluator/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ import {
2929
} from '../../error_messages';
3030
import { LoggerFacade } from '../../logging/logger';
3131

32-
const MODULE_NAME = 'CUSTOM_ATTRIBUTE_CONDITION_EVALUATOR';
33-
3432
const EXACT_MATCH_TYPE = 'exact';
3533
const EXISTS_MATCH_TYPE = 'exists';
3634
const GREATER_OR_EQUAL_THAN_MATCH_TYPE = 'ge';

lib/core/decision_service/index.tests.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,31 @@ import {
3939
getTestProjectConfig,
4040
getTestProjectConfigWithFeatures,
4141
} from '../../tests/test_data';
42+
4243
import {
43-
AUDIENCE_EVALUATION_RESULT_COMBINED,
44-
EVALUATING_AUDIENCES_COMBINED,
45-
USER_FORCED_IN_VARIATION,
4644
USER_HAS_NO_FORCED_VARIATION,
47-
USER_DOESNT_MEET_CONDITIONS_FOR_TARGETING_RULE,
48-
USER_NOT_IN_EXPERIMENT,
45+
VALID_BUCKETING_ID,
46+
SAVED_USER_VARIATION,
47+
SAVED_VARIATION_NOT_FOUND,
48+
} from '../../log_messages';
49+
50+
import {
4951
EXPERIMENT_NOT_RUNNING,
5052
RETURNING_STORED_VARIATION,
53+
USER_NOT_IN_EXPERIMENT,
54+
USER_FORCED_IN_VARIATION,
55+
EVALUATING_AUDIENCES_COMBINED,
56+
AUDIENCE_EVALUATION_RESULT_COMBINED,
57+
USER_IN_ROLLOUT,
58+
USER_NOT_IN_ROLLOUT,
5159
FEATURE_HAS_NO_EXPERIMENTS,
52-
NO_ROLLOUT_EXISTS,
60+
USER_DOESNT_MEET_CONDITIONS_FOR_TARGETING_RULE,
61+
USER_NOT_BUCKETED_INTO_TARGETING_RULE,
5362
USER_BUCKETED_INTO_TARGETING_RULE,
54-
USER_IN_ROLLOUT,
63+
NO_ROLLOUT_EXISTS,
5564
USER_MEETS_CONDITIONS_FOR_TARGETING_RULE,
56-
USER_NOT_BUCKETED_INTO_TARGETING_RULE,
57-
USER_NOT_IN_ROLLOUT,
58-
VALID_BUCKETING_ID,
59-
SAVED_USER_VARIATION,
60-
SAVED_VARIATION_NOT_FOUND
61-
} from '../../log_messages';
62-
import { mock } from 'node:test';
65+
} from '../decision_service/index';
66+
6367
import { BUCKETING_ID_NOT_STRING, USER_PROFILE_LOOKUP_ERROR, USER_PROFILE_SAVE_ERROR } from '../../error_messages';
6468

6569
var testData = getTestProjectConfig();

0 commit comments

Comments
 (0)