Skip to content

Commit

Permalink
Add buf check.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnaoto committed Dec 10, 2022
1 parent b17008d commit 07f9915
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package deploygate

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -113,18 +114,25 @@ func (c *Client) NewRequest(httpRequest *HttpRequest) (*http.Response, error) {
func (c *Client) Decode(resp *http.Response, out interface{}) error {
defer resp.Body.Close()

buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)

if resp.StatusCode >= 300 {
return fmt.Errorf("status code is %v, body is %v", resp.StatusCode, resp.Body)
return fmt.Errorf("status code is %v, body is %v", resp.StatusCode, buf.String())
}

if resp.ContentLength == 0 {
return nil
}

err := json.NewDecoder(resp.Body).Decode(&out)
if buf.String() == "" {
return nil
}

err := json.Unmarshal(buf.Bytes(), &out)

if err != nil {
return fmt.Errorf("%v, output type is %T, body is %v", err, out, resp.Body)
return fmt.Errorf("%v, output type is %T, body is %v", err, out, buf.String())
}

return nil
Expand Down

0 comments on commit 07f9915

Please sign in to comment.