Make fs.CheckClose public to stop duplication
This commit is contained in:
parent
7a24532224
commit
ac65d8369e
4 changed files with 6 additions and 25 deletions
13
rest/rest.go
13
rest/rest.go
|
@ -29,7 +29,7 @@ func NewClient(c *http.Client, rootURL string) *Client {
|
|||
|
||||
// defaultErrorHandler doesn't attempt to parse the http body
|
||||
func defaultErrorHandler(resp *http.Response) (err error) {
|
||||
defer checkClose(resp.Body, &err)
|
||||
defer fs.CheckClose(resp.Body, &err)
|
||||
return fmt.Errorf("HTTP error %v (%v) returned", resp.StatusCode, resp.Status)
|
||||
}
|
||||
|
||||
|
@ -52,18 +52,9 @@ type Opts struct {
|
|||
ExtraHeaders map[string]string
|
||||
}
|
||||
|
||||
// checkClose is a utility function used to check the return from
|
||||
// Close in a defer statement.
|
||||
func checkClose(c io.Closer, err *error) {
|
||||
cerr := c.Close()
|
||||
if *err == nil {
|
||||
*err = cerr
|
||||
}
|
||||
}
|
||||
|
||||
// DecodeJSON decodes resp.Body into result
|
||||
func DecodeJSON(resp *http.Response, result interface{}) (err error) {
|
||||
defer checkClose(resp.Body, &err)
|
||||
defer fs.CheckClose(resp.Body, &err)
|
||||
decoder := json.NewDecoder(resp.Body)
|
||||
return decoder.Decode(result)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue