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: logging nil project config, adding more meaningful error messages #220

Merged
merged 2 commits into from
Dec 31, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 16 additions & 8 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package client
import (
"errors"
"fmt"
"reflect"
"runtime/debug"
"strconv"

Expand Down Expand Up @@ -57,7 +58,7 @@ func (o *OptimizelyClient) Activate(experimentKey string, userContext entities.U
default:
err = errors.New("unexpected error")
}
errorMessage := fmt.Sprintf("optimizely SDK is panicking with the error:")
errorMessage := fmt.Sprintf("Activate call, optimizely SDK is panicking with the error:")
logger.Error(errorMessage, err)
logger.Debug(string(debug.Stack()))
}
Expand All @@ -69,7 +70,7 @@ func (o *OptimizelyClient) Activate(experimentKey string, userContext entities.U
return result, err
}

if experimentDecision.Variation != nil {
if experimentDecision.Variation != nil && decisionContext.Experiment != nil {
// send an impression event
result = experimentDecision.Variation.Key
impressionEvent := event.CreateImpressionUserEvent(decisionContext.ProjectConfig, *decisionContext.Experiment, *experimentDecision.Variation, userContext)
Expand All @@ -93,7 +94,7 @@ func (o *OptimizelyClient) IsFeatureEnabled(featureKey string, userContext entit
default:
err = errors.New("unexpected error")
}
errorMessage := fmt.Sprintf("optimizely SDK is panicking with the error:")
errorMessage := fmt.Sprintf("IsFeatureEnabled call, optimizely SDK is panicking with the error:")
logger.Error(errorMessage, err)
logger.Debug(string(debug.Stack()))
}
Expand All @@ -117,7 +118,7 @@ func (o *OptimizelyClient) IsFeatureEnabled(featureKey string, userContext entit
logger.Info(fmt.Sprintf(`Feature "%s" is not enabled for user "%s".`, featureKey, userContext.ID))
}

if featureDecision.Source == decision.FeatureTest {
if featureDecision.Source == decision.FeatureTest && featureDecision.Variation != nil {
// send impression event for feature tests
impressionEvent := event.CreateImpressionUserEvent(decisionContext.ProjectConfig, featureDecision.Experiment, *featureDecision.Variation, userContext)
o.EventProcessor.ProcessEvent(impressionEvent)
Expand All @@ -139,7 +140,7 @@ func (o *OptimizelyClient) GetEnabledFeatures(userContext entities.UserContext)
default:
err = errors.New("unexpected error")
}
errorMessage := fmt.Sprintf("optimizely SDK is panicking with the error:")
errorMessage := fmt.Sprintf("GetEnabledFeatures call, optimizely SDK is panicking with the error:")
logger.Error(errorMessage, err)
logger.Debug(string(debug.Stack()))
}
Expand Down Expand Up @@ -280,7 +281,7 @@ func (o *OptimizelyClient) GetVariation(experimentKey string, userContext entiti
default:
err = errors.New("unexpected error")
}
errorMessage := fmt.Sprintf("optimizely SDK is panicking with the error:")
errorMessage := fmt.Sprintf("GetVariation call, optimizely SDK is panicking with the error:")
logger.Error(errorMessage, err)
logger.Debug(string(debug.Stack()))
}
Expand Down Expand Up @@ -312,7 +313,7 @@ func (o *OptimizelyClient) Track(eventKey string, userContext entities.UserConte
default:
err = errors.New("unexpected error")
}
errorMessage := fmt.Sprintf("optimizely SDK is panicking with the error:")
errorMessage := fmt.Sprintf("Track call, optimizely SDK is panicking with the error:")
logger.Error(errorMessage, err)
logger.Debug(string(debug.Stack()))
}
Expand Down Expand Up @@ -355,7 +356,7 @@ func (o *OptimizelyClient) getFeatureDecision(featureKey, variableKey string, us
default:
err = errors.New("unexpected error")
}
errorMessage := fmt.Sprintf("optimizely SDK is panicking with the error:")
errorMessage := fmt.Sprintf("getFeatureDecision call, optimizely SDK is panicking with the error:")
logger.Error(errorMessage, err)
logger.Debug(string(debug.Stack()))
}
Expand Down Expand Up @@ -477,6 +478,9 @@ func (o *OptimizelyClient) RemoveOnTrack(id int) error {

func (o *OptimizelyClient) getProjectConfig() (projectConfig config.ProjectConfig, err error) {

if isNil(o.ConfigManager) {
return nil, errors.New("project config manager is not initialized")
}
projectConfig, err = o.ConfigManager.GetConfig()
if err != nil {
return nil, err
Expand All @@ -496,3 +500,7 @@ func (o *OptimizelyClient) GetOptimizelyConfig() (optimizelyConfig *config.Optim
func (o *OptimizelyClient) Close() {
o.execGroup.TerminateAndWait()
}

func isNil(v interface{}) bool {
return v == nil || (reflect.ValueOf(v).Kind() == reflect.Ptr && reflect.ValueOf(v).IsNil())
}
16 changes: 16 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func ValidProjectConfigManager() *MockProjectConfigManager {
return p
}

func InValidProjectConfigManager() *MockProjectConfigManager {
return nil
}

type MockProcessor struct {
Events []event.UserEvent
mock.Mock
Expand Down Expand Up @@ -1192,6 +1196,18 @@ func TestGetProjectConfigIsValid(t *testing.T) {
assert.Equal(t, mockConfigManager.projectConfig, actual)
}

func TestGetProjectConfigIsInValid(t *testing.T) {

client := OptimizelyClient{
ConfigManager: InValidProjectConfigManager(),
}

actual, err := client.getProjectConfig()

assert.NotNil(t, err)
assert.Nil(t, actual)
}

func TestGetOptimizelyConfig(t *testing.T) {
mockConfigManager := ValidProjectConfigManager()

Expand Down