response: handle late errors

This commit was moved from ipfs/go-ipfs-http-client@139e9e5ff1
This commit is contained in:
Łukasz Magiera 2019-02-18 17:12:37 +01:00
parent 7229dbbf73
commit b6ace8dd40
2 changed files with 10 additions and 5 deletions

View File

@ -103,11 +103,11 @@ func (r *RequestBuilder) Exec(ctx context.Context, res interface{}) error {
}
if res == nil {
httpRes.Close()
lateErr := httpRes.Close()
if httpRes.Error != nil {
return httpRes.Error
}
return nil
return lateErr
}
return httpRes.Decode(res)

View File

@ -39,9 +39,14 @@ type Response struct {
func (r *Response) Close() error {
if r.Output != nil {
// always drain output (response body)
//ioutil.ReadAll(r.Output) // TODO: might not be a good idea in case there is a lot of data
return r.Output.Close()
// always drain output (response body) //TODO: make optional for things like cat
_, err1 := io.Copy(ioutil.Discard, r.Output)
err2 := r.Output.Close()
if err1 != nil {
return err1
}
return err2
}
return nil
}