Skip to content

Commit

Permalink
fix: rework ReloadDeclarativeRawConfig error path
Browse files Browse the repository at this point in the history
Now that ReloadDeclarativeRawConfig returns config bodies always, the
original error checking path didn't quite make sense. This extracts the
body read and error check from the status error check and removes the
response body from the status error.
  • Loading branch information
rainest committed Feb 3, 2023
1 parent f7b64c0 commit ab5b18a
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions kong/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,15 +405,12 @@ func (c *Client) ReloadDeclarativeRawConfig(
}
defer resp.Body.Close()

var b []byte
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("could not read /config %d status response body: %w", resp.StatusCode, err)
}
if resp.StatusCode < 200 || resp.StatusCode >= 400 {
b, err = io.ReadAll(resp.Body)
if err != nil {
return nil,
fmt.Errorf(`failed posting new config to /config: got status code %d
(and failed to read the response body): %w`,
resp.StatusCode, err)
}
return b, fmt.Errorf("failed posting new config to /config: got status code %d", resp.StatusCode)
}

return b, nil
Expand Down

0 comments on commit ab5b18a

Please sign in to comment.