@@ -31,7 +31,7 @@ type Client struct {
3131 ctxAnalytics context.Context
3232 log Logger
3333 offlineHandler OfflineHandler
34- errorHandler func (handler FlagsmithErrorHandler )
34+ errorHandler func (handler * FlagsmithAPIError )
3535}
3636
3737// NewClient creates instance of Client with given configuration.
@@ -148,7 +148,8 @@ func (c *Client) GetIdentitySegments(identifier string, traits []*Trait) ([]*seg
148148// NOTE: This method only works with Edge API endpoint.
149149func (c * Client ) BulkIdentify (ctx context.Context , batch []* IdentityTraits ) error {
150150 if len (batch ) > bulkIdentifyMaxCount {
151- return & FlagsmithAPIError {msg : fmt .Sprintf ("flagsmith: batch size must be less than %d" , bulkIdentifyMaxCount )}
151+ msg := fmt .Sprintf ("flagsmith: batch size must be less than %d" , bulkIdentifyMaxCount )
152+ return & FlagsmithAPIError {Msg : msg }
152153 }
153154
154155 body := struct {
@@ -161,13 +162,16 @@ func (c *Client) BulkIdentify(ctx context.Context, batch []*IdentityTraits) erro
161162 ForceContentType ("application/json" ).
162163 Post (c .config .baseURL + "bulk-identities/" )
163164 if resp .StatusCode () == 404 {
164- return & FlagsmithAPIError {msg : "flagsmith: Bulk identify endpoint not found; Please make sure you are using Edge API endpoint" }
165+ msg := "flagsmith: Bulk identify endpoint not found; Please make sure you are using Edge API endpoint"
166+ return & FlagsmithAPIError {Msg : msg , Err : err , ResponseStatusCode : resp .StatusCode (), ResponseStatus : resp .Status ()}
165167 }
166168 if err != nil {
167- return & FlagsmithAPIError {msg : fmt .Sprintf ("flagsmith: error performing request to Flagsmith API: %s" , err )}
169+ msg := fmt .Sprintf ("flagsmith: error performing request to Flagsmith API: %s" , err )
170+ return & FlagsmithAPIError {Msg : msg , Err : err , ResponseStatusCode : resp .StatusCode (), ResponseStatus : resp .Status ()}
168171 }
169172 if ! resp .IsSuccess () {
170- return & FlagsmithAPIError {msg : fmt .Sprintf ("flagsmith: unexpected response from Flagsmith API: %s" , resp .Status ())}
173+ msg := fmt .Sprintf ("flagsmith: unexpected response from Flagsmith API: %s" , resp .Status ())
174+ return & FlagsmithAPIError {Msg : msg , Err : err , ResponseStatusCode : resp .StatusCode (), ResponseStatus : resp .Status ()}
171175 }
172176 return nil
173177}
@@ -180,10 +184,12 @@ func (c *Client) GetEnvironmentFlagsFromAPI(ctx context.Context) (Flags, error)
180184 ForceContentType ("application/json" ).
181185 Get (c .config .baseURL + "flags/" )
182186 if err != nil {
183- return Flags {}, & FlagsmithAPIError {msg : fmt .Sprintf ("flagsmith: error performing request to Flagsmith API: %s" , err )}
187+ msg := fmt .Sprintf ("flagsmith: error performing request to Flagsmith API: %s" , err )
188+ return Flags {}, & FlagsmithAPIError {Msg : msg , Err : err , ResponseStatusCode : resp .StatusCode (), ResponseStatus : resp .Status ()}
184189 }
185190 if ! resp .IsSuccess () {
186- return Flags {}, & FlagsmithAPIError {msg : fmt .Sprintf ("flagsmith: unexpected response from Flagsmith API: %s" , resp .Status ())}
191+ msg := fmt .Sprintf ("flagsmith: unexpected response from Flagsmith API: %s" , resp .Status ())
192+ return Flags {}, & FlagsmithAPIError {Msg : msg , Err : err , ResponseStatusCode : resp .StatusCode (), ResponseStatus : resp .Status ()}
187193 }
188194 return makeFlagsFromAPIFlags (resp .Body (), c .analyticsProcessor , c .defaultFlagHandler )
189195}
@@ -201,10 +207,12 @@ func (c *Client) GetIdentityFlagsFromAPI(ctx context.Context, identifier string,
201207 ForceContentType ("application/json" ).
202208 Post (c .config .baseURL + "identities/" )
203209 if err != nil {
204- return Flags {}, & FlagsmithAPIError {msg : fmt .Sprintf ("flagsmith: error performing request to Flagsmith API: %s" , err )}
210+ msg := fmt .Sprintf ("flagsmith: error performing request to Flagsmith API: %s" , err )
211+ return Flags {}, & FlagsmithAPIError {Msg : msg , Err : err , ResponseStatusCode : resp .StatusCode (), ResponseStatus : resp .Status ()}
205212 }
206213 if ! resp .IsSuccess () {
207- return Flags {}, & FlagsmithAPIError {msg : fmt .Sprintf ("flagsmith: unexpected response from Flagsmith API: %s" , resp .Status ())}
214+ msg := fmt .Sprintf ("flagsmith: unexpected response from Flagsmith API: %s" , resp .Status ())
215+ return Flags {}, & FlagsmithAPIError {Msg : msg , Err : err , ResponseStatusCode : resp .StatusCode (), ResponseStatus : resp .Status ()}
208216 }
209217 return makeFlagsfromIdentityAPIJson (resp .Body (), c .analyticsProcessor , c .defaultFlagHandler )
210218}
@@ -268,16 +276,18 @@ func (c *Client) UpdateEnvironment(ctx context.Context) error {
268276 Get (c .config .baseURL + "environment-document/" )
269277
270278 if err != nil {
271- f := & FlagsmithAPIError {msg : fmt .Sprintf ("flagsmith: error performing request to Flagsmith API: %s" , err )}
279+ msg := fmt .Sprintf ("flagsmith: error performing request to Flagsmith API: %s" , err )
280+ f := & FlagsmithAPIError {Msg : msg , Err : err , ResponseStatusCode : resp .StatusCode (), ResponseStatus : resp .Status ()}
272281 if c .errorHandler != nil {
273- c .errorHandler (FlagsmithErrorHandler { err , resp . StatusCode (), resp . Status ()} )
282+ c .errorHandler (f )
274283 }
275284 return f
276285 }
277286 if resp .StatusCode () != 200 {
278- f := & FlagsmithAPIError {msg : fmt .Sprintf ("flagsmith: unexpected response from Flagsmith API: %s" , resp .Status ())}
287+ msg := fmt .Sprintf ("flagsmith: unexpected response from Flagsmith API: %s" , resp .Status ())
288+ f := & FlagsmithAPIError {Msg : msg , Err : err , ResponseStatusCode : resp .StatusCode (), ResponseStatus : resp .Status ()}
279289 if c .errorHandler != nil {
280- c .errorHandler (FlagsmithErrorHandler { err , resp . StatusCode (), resp . Status ()} )
290+ c .errorHandler (f )
281291 }
282292 return f
283293 }
0 commit comments