From 0f9657d1220de0f34b86867e7a9bc1fd681078eb Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Wed, 27 Jan 2016 21:39:50 -0800 Subject: [PATCH] Not trying to parse response if the body is not a json --- api/fs.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/api/fs.go b/api/fs.go index dc8440520..e1331368a 100644 --- a/api/fs.go +++ b/api/fs.go @@ -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 + } +}