Skip to content

Commit b7ff510

Browse files
committed
fix(client): Client won't crash on no response + no error code.
1 parent 4bc0209 commit b7ff510

File tree

2 files changed

+6
-17
lines changed

2 files changed

+6
-17
lines changed

v2/api/client.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,13 @@ func (c *Client) sendRequest(request *request) (*http.Response, error) {
202202
resp, respErr := c.httpClient.Do(req)
203203

204204
// check if context deadline exceeded
205-
if respErr != nil && strings.Contains(respErr.Error(), "context deadline exceeded") || (resp != nil && http.StatusRequestTimeout == resp.StatusCode) {
206-
// retry until max retries reached
205+
switch {
206+
case respErr != nil && (strings.Contains(respErr.Error(), "context deadline exceeded")):
207207
sleepDuration := time.Duration(1) * time.Second
208208
for i := 0; i < MAX_CONTEXT_DEADLINE_RETRIES; i++ {
209209
// sleep for exponential backoff
210210
if i > 0 {
211-
sleepDuration = sleepDuration * 2
211+
sleepDuration *= 2
212212
if sleepDuration > time.Duration(MAX_WAIT_SECONDS)*time.Second {
213213
sleepDuration = time.Duration(MAX_WAIT_SECONDS) * time.Second
214214
}
@@ -226,13 +226,11 @@ func (c *Client) sendRequest(request *request) (*http.Response, error) {
226226
resp = resp2
227227
break
228228
}
229-
respErr = respErr2
230229
}
231-
if resp == nil {
232-
return nil, fmt.Errorf("no response from Keyfactor Command after %d retries", MAX_CONTEXT_DEADLINE_RETRIES)
233-
}
234-
} else if respErr != nil {
230+
case respErr != nil:
235231
return nil, respErr
232+
case resp == nil:
233+
return nil, errors.New("no response from Keyfactor Command")
236234
}
237235
var stringMessage string
238236
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusNoContent {

v2/api/security.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,11 @@ func (c *Client) GetSecurityRoles() ([]GetSecurityRolesResponse, error) {
134134
return nil, err
135135
}
136136

137-
//var jsonRespDebug []GetSecurityRolesResponseDebug
138-
//var rawInterface interface{}
139-
//err = json.NewDecoder(resp.Body).Decode(&rawInterface)
140-
//get first element of the array and cast it to a
141137
var jsonResp []GetSecurityRolesResponse
142138
err = json.NewDecoder(resp.Body).Decode(&jsonResp)
143-
144139
if err != nil {
145140
return nil, err
146141
}
147-
148-
// Convert interface into a slice of GetSecurityRolesResponse
149-
//jsonResp = rawInterface.([]GetSecurityRolesResponse)
150-
151142
return jsonResp, nil
152143
}
153144

0 commit comments

Comments
 (0)