Skip to content

Commit 54ff6e5

Browse files
committed
Review Feedback
* Remove handler from pollEnvironment to UpdateEnvironment * Rename handler to errorHandler * Create a struct FlagsmithErrorHandler so that we can return error and other client response details
1 parent 299546d commit 54ff6e5

File tree

5 files changed

+42
-38
lines changed

5 files changed

+42
-38
lines changed

client.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +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
34-
pollErrorHandler func(error)
29+
client *resty.Client
30+
ctxLocalEval context.Context
31+
ctxAnalytics context.Context
32+
log Logger
33+
offlineHandler OfflineHandler
34+
errorHandler func(handler FlagsmithErrorHandler)
3535
}
3636

3737
// NewClient creates instance of Client with given configuration.
@@ -245,9 +245,6 @@ func (c *Client) pollEnvironment(ctx context.Context) {
245245
err := c.UpdateEnvironment(ctx)
246246
if err != nil {
247247
c.log.Errorf("Failed to update environment: %v", err)
248-
if c.pollErrorHandler != nil {
249-
c.pollErrorHandler(err)
250-
}
251248
}
252249
}
253250
update()
@@ -271,10 +268,18 @@ func (c *Client) UpdateEnvironment(ctx context.Context) error {
271268
Get(c.config.baseURL + "environment-document/")
272269

273270
if err != nil {
274-
return &FlagsmithAPIError{msg: fmt.Sprintf("flagsmith: error performing request to Flagsmith API: %s", err)}
271+
f := &FlagsmithAPIError{msg: fmt.Sprintf("flagsmith: error performing request to Flagsmith API: %s", err)}
272+
if c.errorHandler != nil {
273+
c.errorHandler(FlagsmithErrorHandler{err, resp.StatusCode(), resp.Status()})
274+
}
275+
return f
275276
}
276277
if resp.StatusCode() != 200 {
277-
return &FlagsmithAPIError{msg: fmt.Sprintf("flagsmith: unexpected response from Flagsmith API: %s", resp.Status())}
278+
f := &FlagsmithAPIError{msg: fmt.Sprintf("flagsmith: unexpected response from Flagsmith API: %s", resp.Status())}
279+
if c.errorHandler != nil {
280+
c.errorHandler(FlagsmithErrorHandler{err, resp.StatusCode(), resp.Status()})
281+
}
282+
return f
278283
}
279284
c.environment.Store(&env)
280285
identitiesWithOverrides := make(map[string]identities.IdentityModel)

client_export_test.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

client_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -677,10 +677,11 @@ func TestOfflineHandlerIsUsedWhenRequestFails(t *testing.T) {
677677
}
678678

679679
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
680+
// Given
681+
ctx := context.Background()
682+
var capturedError error
683+
var statusCode int
684+
var status string
684685

685686
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
686687
rw.WriteHeader(http.StatusInternalServerError)
@@ -691,19 +692,18 @@ func TestPollErrorHandlerIsUsedWhenPollFails(t *testing.T) {
691692
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey,
692693
flagsmith.WithBaseURL(server.URL+"/api/v1/"),
693694
flagsmith.WithEnvironmentRefreshInterval(time.Duration(2)*time.Second),
694-
flagsmith.WithPollErrorHandler(func(err error) {
695-
capturedError = err
695+
flagsmith.WithErrorHandler(func(handler flagsmith.FlagsmithErrorHandler) {
696+
capturedError = handler.Err
697+
statusCode = handler.ResponseStatusCode
698+
status = handler.ResponseStatus
696699
}),
697700
)
698701

699-
// when
700-
go func() { flagsmith.PollEnvironment(client, ctx) }()
702+
// when
703+
_ = client.UpdateEnvironment(ctx)
701704

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)
705+
// Then
706+
assert.Equal(t, capturedError, nil)
707+
assert.Equal(t, statusCode, 500)
708+
assert.Equal(t, status, "500 Internal Server Error")
709709
}

errors.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ type FlagsmithAPIError struct {
88
msg string
99
}
1010

11+
type FlagsmithErrorHandler struct {
12+
Err error
13+
ResponseStatusCode int
14+
ResponseStatus string
15+
}
16+
1117
func (e FlagsmithClientError) Error() string {
1218
return e.msg
1319
}

options.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ func WithOfflineMode() Option {
118118
}
119119
}
120120

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-
}
121+
// WithErrorHandler provides a way to handle errors that occur during update of an environment
122+
func WithErrorHandler(handler func(handler FlagsmithErrorHandler)) Option {
123+
return func(c *Client) {
124+
c.errorHandler = handler
125+
}
126126
}

0 commit comments

Comments
 (0)