Skip to content

Commit

Permalink
Wrap error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-harrison committed Apr 11, 2024
1 parent e592264 commit 4d81906
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 26 deletions.
22 changes: 11 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (c *Client) GetEnvironmentFlags(ctx context.Context) (f Flags, err error) {
} else if c.defaultFlagHandler != nil {
return Flags{defaultFlagHandler: c.defaultFlagHandler}, nil
}
return Flags{}, &FlagsmithClientError{msg: fmt.Sprintf("Failed to fetch flags with error: %s", err)}
return Flags{}, FlagsmithClientError(fmt.Errorf("Failed to fetch flags with error: %w", err))
}

// Returns `Flags` struct holding all the flags for the current environment for
Expand Down Expand Up @@ -133,7 +133,7 @@ func (c *Client) GetIdentityFlags(ctx context.Context, identifier string, traits
} else if c.defaultFlagHandler != nil {
return Flags{defaultFlagHandler: c.defaultFlagHandler}, nil
}
return Flags{}, &FlagsmithClientError{msg: fmt.Sprintf("Failed to fetch flags with error: %s", err)}
return Flags{}, FlagsmithClientError(fmt.Errorf("Failed to fetch flags with error: %w", err))
}

// Returns an array of segments that the given identity is part of.
Expand All @@ -142,14 +142,14 @@ func (c *Client) GetIdentitySegments(identifier string, traits []*Trait) ([]*seg
identity := c.getIdentityModel(identifier, env.APIKey, traits)
return flagengine.GetIdentitySegments(env, &identity), nil
}
return nil, &FlagsmithClientError{msg: "flagsmith: Local evaluation required to obtain identity segments"}
return nil, FlagsmithClientError(errors.New("flagsmith: Local evaluation required to obtain identity segments"))
}

// BulkIdentify can be used to create/overwrite identities(with traits) in bulk
// NOTE: This method only works with Edge API endpoint.
func (c *Client) BulkIdentify(ctx context.Context, batch []*IdentityTraits) error {
if len(batch) > bulkIdentifyMaxCount {
return &FlagsmithAPIError{msg: fmt.Sprintf("flagsmith: batch size must be less than %d", bulkIdentifyMaxCount)}
return FlagsmithAPIError(fmt.Errorf("flagsmith: batch size must be less than %d", bulkIdentifyMaxCount))
}

body := struct {
Expand All @@ -162,13 +162,13 @@ func (c *Client) BulkIdentify(ctx context.Context, batch []*IdentityTraits) erro
ForceContentType("application/json").
Post(c.config.baseURL + "bulk-identities/")
if resp.StatusCode() == 404 {
return &FlagsmithAPIError{msg: "flagsmith: Bulk identify endpoint not found; Please make sure you are using Edge API endpoint"}
return FlagsmithAPIError(errors.New("flagsmith: Bulk identify endpoint not found; Please make sure you are using Edge API endpoint"))
}
if err != nil {
return &FlagsmithAPIError{msg: fmt.Sprintf("flagsmith: error performing request to Flagsmith API: %s", err)}
return FlagsmithAPIError(fmt.Errorf("flagsmith: error performing request to Flagsmith API: %w", err))
}
if !resp.IsSuccess() {
return &FlagsmithAPIError{msg: fmt.Sprintf("flagsmith: unexpected response from Flagsmith API: %s", resp.Status())}
return FlagsmithAPIError(fmt.Errorf("flagsmith: unexpected response from Flagsmith API: %s", resp.Status()))
}
return nil
}
Expand All @@ -181,10 +181,10 @@ func (c *Client) GetEnvironmentFlagsFromAPI(ctx context.Context) (Flags, error)
ForceContentType("application/json").
Get(c.config.baseURL + "flags/")
if err != nil {
return Flags{}, &FlagsmithAPIError{msg: fmt.Sprintf("flagsmith: error performing request to Flagsmith API: %s", err)}
return Flags{}, FlagsmithAPIError(fmt.Errorf("flagsmith: error performing request to Flagsmith API: %w", err))
}
if !resp.IsSuccess() {
return Flags{}, &FlagsmithAPIError{msg: fmt.Sprintf("flagsmith: unexpected response from Flagsmith API: %s", resp.Status())}
return Flags{}, FlagsmithAPIError(fmt.Errorf("flagsmith: unexpected response from Flagsmith API: %s", resp.Status()))
}
return makeFlagsFromAPIFlags(resp.Body(), c.analyticsProcessor, c.defaultFlagHandler)
}
Expand All @@ -202,10 +202,10 @@ func (c *Client) GetIdentityFlagsFromAPI(ctx context.Context, identifier string,
ForceContentType("application/json").
Post(c.config.baseURL + "identities/")
if err != nil {
return Flags{}, &FlagsmithAPIError{msg: fmt.Sprintf("flagsmith: error performing request to Flagsmith API: %s", err)}
return Flags{}, FlagsmithAPIError(fmt.Errorf("flagsmith: error performing request to Flagsmith API: %w", err))
}
if !resp.IsSuccess() {
return Flags{}, &FlagsmithAPIError{msg: fmt.Sprintf("flagsmith: unexpected response from Flagsmith API: %s", resp.Status())}
return Flags{}, FlagsmithAPIError(fmt.Errorf("flagsmith: unexpected response from Flagsmith API: %s", resp.Status()))
}
return makeFlagsfromIdentityAPIJson(resp.Body(), c.analyticsProcessor, c.defaultFlagHandler)
}
Expand Down
16 changes: 2 additions & 14 deletions errors.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
package flagsmith

type FlagsmithClientError struct {
msg string
}
type FlagsmithClientError error

type FlagsmithAPIError struct {
msg string
}

func (e FlagsmithClientError) Error() string {
return e.msg
}

func (e FlagsmithAPIError) Error() string {
return e.msg
}
type FlagsmithAPIError error
2 changes: 1 addition & 1 deletion models.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (f *Flags) GetFlag(featureName string) (Flag, error) {
if f.defaultFlagHandler != nil {
return f.defaultFlagHandler(featureName)
}
return resultFlag, &FlagsmithClientError{fmt.Sprintf("flagsmith: No feature found with name %q", featureName)}
return resultFlag, FlagsmithClientError(fmt.Errorf("flagsmith: No feature found with name %q", featureName))
}
if f.analyticsProcessor != nil {
f.analyticsProcessor.TrackFeature(resultFlag.FeatureName)
Expand Down

0 comments on commit 4d81906

Please sign in to comment.