8000 throw error from createUserContext in case of invalid input by raju-opti · Pull Request #1063 · optimizely/javascript-sdk · GitHub
[go: up one dir, main page]

Skip to content

throw error from createUserContext in case of invalid input #1063

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

Merged
merged 4 commits into from
May 28, 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
24 changes: 13 additions & 11 deletions lib/optimizely/index.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { assert, expect } from 'chai';
import sinon from 'sinon';
import { sprintf } from '../utils/fns';
import { NOTIFICATION_TYPES } from '../notification_center/type';
import Optimizely from './';
import Optimizely, { INVALID_ATTRIBUTES, INVALID_IDENTIFIER } from './';
import OptimizelyUserContext from '../optimizely_user_context';
import { OptimizelyDecideOption } from '../shared_types';
import AudienceEvaluator from '../core/audience_evaluator';
Expand Down Expand Up @@ -4379,14 +4379,16 @@ describe('lib/optimizely', function() {
assert.deepEqual(userId, user.getUserId());
});

it('should return null OptimizelyUserContext when input userId is null', function() {
var user = optlyInstance.createUserContext(null);
assert.deepEqual(null, user);
it('should throw error when input userId is null', function() {
assert.throws(() => {
optlyInstance.createUserContext(null);
}, Error, INVALID_IDENTIFIER);
});

it('should return null OptimizelyUserContext when input userId is undefined', function() {
var user = optlyInstance.createUserContext(undefined);
assert.deepEqual(null, user);
it('should throw error when input userId is undefined', function() {
assert.throws(() => {
optlyInstance.createUserContext(undefined);
}, Error, INVALID_IDENTIFIER);
});

it('should create multiple instances of OptimizelyUserContext', function() {
Expand All @@ -4405,11 +4407,11 @@ describe('lib/optimizely', function() {
assert.deepEqual(user2.getUserId(), userId2);
});

it('should call the error handler for invalid user ID and return null', function() {
it('should call the error handler for invalid user ID and throw', function() {
const { optlyInstance, errorNotifier, createdLogger } = getOptlyInstance({
datafileObj: testData.getTestDecideProjectConfig(),
});
assert.isNull(optlyInstance.createUserContext(1));
assert.throws(() => optlyInstance.createUserContext(1), Error, INVALID_IDENTIFIER);
sinon.assert.calledOnce(errorNotifier.notify);
// var errorMessage = errorHandler.handleError.lastCall.args[0].message;
// assert.strictEqual(errorMessage, sprintf(INVALID_INPUT_FORMAT, 'OPTIMIZELY', 'user_id'));
Expand All @@ -4418,11 +4420,11 @@ describe('lib/optimizely', function() {
// assert.strictEqual(logMessage, sprintf(INVALID_INPUT_FORMAT, 'OPTIMIZELY', 'user_id'));
});

it('should call the error handler for invalid attributes and return null', function() {
it('should call the error handler for invalid attributes and throw', function() {
const { optlyInstance, errorNotifier, createdLogger } = getOptlyInstance({
datafileObj: testData.getTestDecideProjectConfig(),
});
assert.isNull(optlyInstance.createUserContext('user1', 'invalid_attributes'));
assert.throws(() => optlyInstance.createUserContext('user1', 'invalid_attributes'), Error, INVALID_ATTRIBUTES);
sinon.assert.calledOnce(errorNotifier.notify);
// var errorMessage = errorHandler.handleError.lastCall.args[0].message;
// assert.strictEqual(errorMessage, sprintf(INVALID_ATTRIBUTES, 'ATTRIBUTES_VALIDATOR'));
Expand Down
14 changes: 10 additions & 4 deletions lib/optimizely/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ type DecisionReasons = (string | number)[];

export const INSTANCE_CLOSED = 'Instance closed';
export const ONREADY_TIMEOUT = 'onReady timeout expired after %s ms';
export const INVALID_IDENTIFIER = 'Invalid identifier';
export const INVALID_ATTRIBUTES = 'Invalid attributes';

/**
* options required to create optimizely object
Expand Down Expand Up @@ -1356,13 +1358,17 @@ export default class Optimizely extends BaseService implements Client {
* @param {string} userId (Optional) The user ID to be used for bucketing.
* @param {UserAttributes} attributes (Optional) user attributes.
* @return {OptimizelyUserContext|null} An OptimizelyUserContext associated with this OptimizelyClient or
* null if provided inputs are invalid
* throws if provided inputs are invalid
*/
createUserContext(userId?: string, attributes?: UserAttributes): OptimizelyUserContext | null {
createUserContext(userId?: string, attributes?: UserAttributes): OptimizelyUserContext {
const userIdentifier = userId ?? this.vuidManager?.getVuid();

if (userIdentifier === undefined || !this.validateInputs({ user_id: userIdentifier }, attributes)) {
return null;
if (userIdentifier === undefined || !this.validateInputs({ user_id: userIdentifier })) {
throw new Error(INVALID_IDENTIFIER);
}

if (!this.validateInputs({}, attributes)) {
throw new Error(INVALID_ATTRIBUTES);
}

const userContext = new OptimizelyUserContext({
Expand Down
2 changes: 1 addition & 1 deletion lib/shared_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export interface OptimizelyVariable {
export interface Client {
// TODO: In the future, will add a function to allow overriding the VUID.
getVuid(): string | undefined;
createUserContext(userId?: string, attributes?: UserAttributes): OptimizelyUserContext | null;
createUserContext(userId?: string, attributes?: UserAttributes): OptimizelyUserContext;
notificationCenter: NotificationCenter;
activate(experimentKey: string, userId: string, attributes?: UserAttributes): string | null;
track(eventKey: string, userId: string, attributes?: UserAttributes, eventTags?: EventTags): void;
Expand Down
Loading
0