Skip to content
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

fix: React SDK null client handling #149

Merged
merged 10 commits into from
Feb 28, 2022
Merged

Conversation

opti-jnguyen
Copy link
Contributor

@opti-jnguyen opti-jnguyen commented Feb 11, 2022

Summary

Handle React SDK fail catching if ._client returns null when creating new ReactClientSDK instance from createInstance function.

Test Plan

Manually tested thoroughly.
All existing unit tests pass.

Issues

#134

@opti-jnguyen opti-jnguyen changed the title Jnguyen/client null handling fix: React SDK null client handling Feb 11, 2022
@opti-jnguyen opti-jnguyen marked this pull request as draft February 11, 2022 19:12
@opti-jnguyen opti-jnguyen marked this pull request as ready for review February 24, 2022 21:03
Copy link
Contributor

@zashraf1985 zashraf1985 left a comment

Choose a reason for hiding this comment

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

Great Work! Its looking really good. I have requested a few minor changes. After that, this one is good to go!

src/client.ts Outdated
export type OnReadyResult = {
success: boolean;
reason?: string;
reason?: NotReadyReason;
reasonDetail?: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

A Non binding suggestion: Does it make sense to call it message instead of reasonDetail? This will make the success case look more inuitive where we dont provide a reason but only reasonDetail right now. But its just a personal preference.. Totally non binding.

return;
}

return this._client.track(eventKey, user.id, user.attributes, eventTags);
this._client.track(eventKey, user.id, user.attributes, eventTags);
Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch! This was an unnecessary return 👍

src/client.ts Outdated
@@ -581,7 +651,7 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
*/
public removeAllForcedDecisions(): boolean {
if (!this.userContext) {
logger.info("Can't remove a forced decision because the user context has not been set yet");
logger.warn('Unable to remove a forced decision because the user context has not been set yet.');
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
logger.warn('Unable to remove a forced decision because the user context has not been set yet.');
logger.warn('Unable to remove forced decisions because the user context has not been set yet.');

src/client.ts Outdated
const user = this.getUserContextWithOverrides(overrideUserId, overrideAttributes);
if (user.id === null) {
logger.info('isFeatureEnabled returning false for feature "%s" because userId is not set', feature);
logger.warn('Unable to determine if feature "%s" is enabled because User ID is not set', feature);
Copy link
Contributor

Choose a reason for hiding this comment

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

Log message nicely rewritten 👍

src/client.ts Outdated
const feature = optlyConfig.featuresMap[featureKey];
if (!feature) {
logger.warn(
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest to convert that to info. Its probably not an unusual behavior.

src/client.ts Outdated
if (user.id === null) {
logger.warn('Unable to get feature variable boolean from feature "%s" because User ID is not set', feature);
Copy link
Contributor

Choose a reason for hiding this comment

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

Convert to info

src/client.ts Outdated
if (user.id === null) {
logger.warn('Unable to get feature variable integer from feature "%s" because User ID is not set', feature);
Copy link
Contributor

Choose a reason for hiding this comment

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

make it info

@@ -887,24 +1096,66 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
* @returns {optimizely.OptimizelyConfig | null} optimizely config
*/
public getOptimizelyConfig(): optimizely.OptimizelyConfig | null {
if (!this._client) {
logger.warn('Unable to get Optimizely configuration because Optimizely client failed to initialize.');
Copy link
Contributor

Choose a reason for hiding this comment

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

Lets call it config. We never use the full term configuration throughout our ecosystem

src/hooks.ts Outdated
switch (res.reason) {
// Optimizely client failed to initialize.
case 'NO_CLIENT':
hooksLogger.info(`Client not ready, reason="${res.reasonDetail}"`);
Copy link
Contributor

Choose a reason for hiding this comment

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

We can probably convert this one to warn. what do you think?

src/hooks.ts Outdated
@@ -431,5 +452,6 @@ export const useDecision: UseDecision = (flagKey, options = {}, overrides = {})
return (): void => {};
}, [optimizely.getIsReadyPromiseFulfilled(), options.autoUpdate, optimizely, flagKey, getCurrentDecision]);

// CHECK: Return clientReady as false & didTimeout as true if ._client is null.
Copy link
Contributor

Choose a reason for hiding this comment

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

This was probably your own note that you forgot to delete?

@Develliot
Copy link

Develliot commented Feb 28, 2022

would it also be to have the Provider silently fails if the optimizely prop (ReactSDKClient) is null when createInstance() fails or isn't possible at time of render, to make it less brittle.

I believe It's got an update handler to handle optimizely prop being added later, it would be nice if the useDecision in stead of throwing an error without a ReactSDKClient so it returned {data, error, isLoading}.

Copy link
Contributor

@zashraf1985 zashraf1985 left a comment

Choose a reason for hiding this comment

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

LGTM! Great Work.

@opti-jnguyen opti-jnguyen merged commit 1663d01 into master Feb 28, 2022
@opti-jnguyen opti-jnguyen deleted the jnguyen/client-null-handling branch February 28, 2022 17:45
@opti-jnguyen
Copy link
Contributor Author

would it also be to have the Provider silently fails if the optimizely prop (ReactSDKClient) is null when createInstance() fails or isn't possible at time of render, to make it less brittle.

I believe It's got an update handler to handle optimizely prop being added later, it would be nice if the useDecision in stead of throwing an error without a ReactSDKClient so it returned {data, error, isLoading}.

@Develliot - thanks for the suggestion! We discussed this approach internally and due to the nature of the change, we will consider the change as a new feature request as it requires refactoring many components.

Our suggestion is to add it to our feature request board here - we will prioritize it based on the number of votes it gets!

Best,
John

} else {
logger.warn('Unable to resolve datafile and user information because Optimizely client failed to initialize.');

this.dataReadyPromise = new Promise((resolve, reject) => {
Copy link

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @fabb - we'll implement that change in a future push!

@opti-jnguyen opti-jnguyen restored the jnguyen/client-null-handling branch March 7, 2022 19:28
@opti-jnguyen opti-jnguyen deleted the jnguyen/client-null-handling branch March 7, 2022 19:30
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.

4 participants