Skip to content

Commit 299546d

Browse files
committed
feat: Add pollErrorHandler to handle pollEnvironment errors
1 parent c3512cf commit 299546d

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

client.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ type Client struct {
2626
analyticsProcessor *AnalyticsProcessor
2727
defaultFlagHandler func(string) (Flag, error)
2828

29-
client *resty.Client
30-
ctxLocalEval context.Context
31-
ctxAnalytics context.Context
32-
log Logger
33-
offlineHandler OfflineHandler
29+
client *resty.Client
30+
ctxLocalEval context.Context
31+
ctxAnalytics context.Context
32+
log Logger
33+
offlineHandler OfflineHandler
34+
pollErrorHandler func(error)
3435
}
3536

3637
// NewClient creates instance of Client with given configuration.
@@ -244,6 +245,9 @@ func (c *Client) pollEnvironment(ctx context.Context) {
244245
err := c.UpdateEnvironment(ctx)
245246
if err != nil {
246247
c.log.Errorf("Failed to update environment: %v", err)
248+
if c.pollErrorHandler != nil {
249+
c.pollErrorHandler(err)
250+
}
247251
}
248252
}
249253
update()

client_export_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package flagsmith
2+
3+
import "context"
4+
5+
func PollEnvironment(client *Client, ctx context.Context) {
6+
client.pollEnvironment(ctx)
7+
}

client_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,3 +675,35 @@ func TestOfflineHandlerIsUsedWhenRequestFails(t *testing.T) {
675675
assert.Equal(t, fixtures.Feature1ID, allFlags[0].FeatureID)
676676
assert.Equal(t, fixtures.Feature1Value, allFlags[0].Value)
677677
}
678+
679+
func TestPollErrorHandlerIsUsedWhenPollFails(t *testing.T) {
680+
// Given
681+
ctx := context.Background()
682+
expectedErrorMessage := "flagsmith: unexpected response from Flagsmith API: 500 Internal Server Error"
683+
var capturedError error
684+
685+
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
686+
rw.WriteHeader(http.StatusInternalServerError)
687+
}))
688+
defer server.Close()
689+
690+
// When
691+
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey,
692+
flagsmith.WithBaseURL(server.URL+"/api/v1/"),
693+
flagsmith.WithEnvironmentRefreshInterval(time.Duration(2)*time.Second),
694+
flagsmith.WithPollErrorHandler(func(err error) {
695+
capturedError = err
696+
}),
697+
)
698+
699+
// when
700+
go func() { flagsmith.PollEnvironment(client, ctx) }()
701+
702+
// stop method in 2 seconds
703+
time.Sleep(1 * time.Second)
704+
ctx.Done()
705+
706+
// Then
707+
assert.NotNil(t, capturedError)
708+
assert.Contains(t, capturedError.Error(), expectedErrorMessage)
709+
}

options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,10 @@ func WithOfflineMode() Option {
117117
c.config.offlineMode = true
118118
}
119119
}
120+
121+
// WithPollErrorHandler provides a way to handle errors that occur during polling of environment
122+
func WithPollErrorHandler(handler func(err error)) Option {
123+
return func(c *Client) {
124+
c.pollErrorHandler = handler
125+
}
126+
}

0 commit comments

Comments
 (0)