Skip to content

Commit 172b0ec

Browse files
fix: logging nil project config, adding more meaningful error messages (#220)
* fix: logging nil project config, adding more meaningful error messages
1 parent 688a80e commit 172b0ec

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

pkg/client/client.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package client
2020
import (
2121
"errors"
2222
"fmt"
23+
"reflect"
2324
"runtime/debug"
2425
"strconv"
2526

@@ -57,7 +58,7 @@ func (o *OptimizelyClient) Activate(experimentKey string, userContext entities.U
5758
default:
5859
err = errors.New("unexpected error")
5960
}
60-
errorMessage := fmt.Sprintf("optimizely SDK is panicking with the error:")
61+
errorMessage := fmt.Sprintf("Activate call, optimizely SDK is panicking with the error:")
6162
logger.Error(errorMessage, err)
6263
logger.Debug(string(debug.Stack()))
6364
}
@@ -69,7 +70,7 @@ func (o *OptimizelyClient) Activate(experimentKey string, userContext entities.U
6970
return result, err
7071
}
7172

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

120-
if featureDecision.Source == decision.FeatureTest {
121+
if featureDecision.Source == decision.FeatureTest && featureDecision.Variation != nil {
121122
// send impression event for feature tests
122123
impressionEvent := event.CreateImpressionUserEvent(decisionContext.ProjectConfig, featureDecision.Experiment, *featureDecision.Variation, userContext)
123124
o.EventProcessor.ProcessEvent(impressionEvent)
@@ -139,7 +140,7 @@ func (o *OptimizelyClient) GetEnabledFeatures(userContext entities.UserContext)
139140
default:
140141
err = errors.New("unexpected error")
141142
}
142-
errorMessage := fmt.Sprintf("optimizely SDK is panicking with the error:")
143+
errorMessage := fmt.Sprintf("GetEnabledFeatures call, optimizely SDK is panicking with the error:")
143144
logger.Error(errorMessage, err)
144145
logger.Debug(string(debug.Stack()))
145146
}
@@ -280,7 +281,7 @@ func (o *OptimizelyClient) GetVariation(experimentKey string, userContext entiti
280281
default:
281282
err = errors.New("unexpected error")
282283
}
283-
errorMessage := fmt.Sprintf("optimizely SDK is panicking with the error:")
284+
errorMessage := fmt.Sprintf("GetVariation call, optimizely SDK is panicking with the error:")
284285
logger.Error(errorMessage, err)
285286
logger.Debug(string(debug.Stack()))
286287
}
@@ -312,7 +313,7 @@ func (o *OptimizelyClient) Track(eventKey string, userContext entities.UserConte
312313
default:
313314
err = errors.New("unexpected error")
314315
}
315-
errorMessage := fmt.Sprintf("optimizely SDK is panicking with the error:")
316+
errorMessage := fmt.Sprintf("Track call, optimizely SDK is panicking with the error:")
316317
logger.Error(errorMessage, err)
317318
logger.Debug(string(debug.Stack()))
318319
}
@@ -355,7 +356,7 @@ func (o *OptimizelyClient) getFeatureDecision(featureKey, variableKey string, us
355356
default:
356357
err = errors.New("unexpected error")
357358
}
358-
errorMessage := fmt.Sprintf("optimizely SDK is panicking with the error:")
359+
errorMessage := fmt.Sprintf("getFeatureDecision call, optimizely SDK is panicking with the error:")
359360
logger.Error(errorMessage, err)
360361
logger.Debug(string(debug.Stack()))
361362
}
@@ -477,6 +478,9 @@ func (o *OptimizelyClient) RemoveOnTrack(id int) error {
477478

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

481+
if isNil(o.ConfigManager) {
482+
return nil, errors.New("project config manager is not initialized")
483+
}
480484
projectConfig, err = o.ConfigManager.GetConfig()
481485
if err != nil {
482486
return nil, err
@@ -496,3 +500,7 @@ func (o *OptimizelyClient) GetOptimizelyConfig() (optimizelyConfig *config.Optim
496500
func (o *OptimizelyClient) Close() {
497501
o.execGroup.TerminateAndWait()
498502
}
503+
504+
func isNil(v interface{}) bool {
505+
return v == nil || (reflect.ValueOf(v).Kind() == reflect.Ptr && reflect.ValueOf(v).IsNil())
506+
}

pkg/client/client_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ func ValidProjectConfigManager() *MockProjectConfigManager {
4141
return p
4242
}
4343

44+
func InValidProjectConfigManager() *MockProjectConfigManager {
45+
return nil
46+
}
47+
4448
type MockProcessor struct {
4549
Events []event.UserEvent
4650
mock.Mock
@@ -1192,6 +1196,18 @@ func TestGetProjectConfigIsValid(t *testing.T) {
11921196
assert.Equal(t, mockConfigManager.projectConfig, actual)
11931197
}
11941198

1199+
func TestGetProjectConfigIsInValid(t *testing.T) {
1200+
1201+
client := OptimizelyClient{
1202+
ConfigManager: InValidProjectConfigManager(),
1203+
}
1204+
1205+
actual, err := client.getProjectConfig()
1206+
1207+
assert.NotNil(t, err)
1208+
assert.Nil(t, actual)
1209+
}
1210+
11951211
func TestGetOptimizelyConfig(t *testing.T) {
11961212
mockConfigManager := ValidProjectConfigManager()
11971213

0 commit comments

Comments
 (0)