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

Conversation

raju-opti
Copy link
Contributor

Summary

Test plan

-updated tests

Issues

@coveralls
Copy link

coveralls commented May 28, 2025

Coverage Status

coverage: 79.753% (+0.02%) from 79.736%
when pulling 8a62b66 on raju/context
into e3234c7 on master.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR updates the createUserContext function to throw errors for invalid inputs instead of returning null.

  • Update the return type in shared_types.ts to indicate a non-null OptimizelyUserContext
  • Modify the error handling in Optimizely.createUserContext to throw errors with specific messages
  • Update tests in index.tests.js to verify that errors are thrown for invalid inputs

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
lib/shared_types.ts Changed createUserContext return type from nullable to non-null to match new behavior
lib/optimizely/index.ts Updated createUserContext to throw errors (INVALID_IDENTIFIER or INVALID_ATTRIBUTES)
lib/optimizely/index.tests.js Modified tests to assert that errors are thrown instead of returning null

Comment on lines 1366 to 1373
if (userIdentifier === undefined || !this.validateInputs({ user_id: userIdentifier })) {
throw new Error(INVALID_IDENTIFIER);
}

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

Copy link
Preview

Copilot AI May 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider reviewing the separation of input validation into two distinct checks; if combining them is feasible without losing error specificity, it might simplify the flow.

Suggested change
if (userIdentifier === undefined || !this.validateInputs({ user_id: userIdentifier })) {
throw new Error(INVALID_IDENTIFIER);
}
if (!this.validateInputs({ user_id: userIdentifier }, attributes)) {
throw new Error(INVALID_ATTRIBUTES);
}
if (
userIdentifier === undefined ||
!this.validateInputs({ user_id: userIdentifier }, attributes)
) {
throw new Error(
userIdentifier === undefined || !this.validateInputs({ user_id: userIdentifier })
? INVALID_IDENTIFIER
: INVALID_ATTRIBUTES
);
}

Copilot uses AI. Check for mistakes.

@raju-opti raju-opti merged commit 8fa0c9b into master May 28, 2025
18 of 19 checks passed
@raju-opti raju-opti deleted the raju/context branch May 28, 2025 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants