-
Notifications
You must be signed in to change notification settings - Fork 83
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
Conversation
There was a problem hiding this 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 |
lib/optimizely/index.ts
Outdated
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); | ||
} | ||
|
There was a problem hiding this comment.
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.
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.
Summary
Test plan
-updated tests
Issues