Not trying to parse response if the body is not a json

This commit is contained in:
Diptanu Choudhury
2016-01-27 21:39:50 -08:00
parent 3bec55e0af
commit 0f9657d122

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
@@ -56,6 +57,9 @@ func (a *AllocFS) List(alloc *Allocation, path string, q *QueryOptions) ([]*Allo
if err != nil {
return nil, nil, err
}
if resp.StatusCode != 200 {
return nil, nil, a.getErrorMsg(resp)
}
decoder := json.NewDecoder(resp.Body)
var files []*AllocFileInfo
if err := decoder.Decode(&files); err != nil {
@@ -91,6 +95,9 @@ func (a *AllocFS) Stat(alloc *Allocation, path string, q *QueryOptions) (*AllocF
if err != nil {
return nil, nil, err
}
if resp.StatusCode != 200 {
return nil, nil, a.getErrorMsg(resp)
}
decoder := json.NewDecoder(resp.Body)
var file *AllocFileInfo
if err := decoder.Decode(&file); err != nil {
@@ -131,3 +138,11 @@ func (a *AllocFS) ReadAt(alloc *Allocation, path string, offset int64, limit int
}
return resp.Body, nil, nil
}
func (a *AllocFS) getErrorMsg(resp *http.Response) error {
if errMsg, err := ioutil.ReadAll(resp.Body); err == nil {
return fmt.Errorf(string(errMsg))
} else {
return err
}
}