mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 18:35:44 +03:00
Not trying to parse response if the body is not a json
This commit is contained in:
15
api/fs.go
15
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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user