resolves issues where failed api calls lead to obscure errors (#7051)
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package ecs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
@@ -50,10 +53,16 @@ func (c *EcsClient) Task() (*Task, error) {
|
||||
|
||||
req, _ := http.NewRequest("GET", c.taskURL, nil)
|
||||
resp, err := c.client.Do(req)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
// ignore the err here; LimitReader returns io.EOF and we're not interested in read errors.
|
||||
body, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 200))
|
||||
return nil, fmt.Errorf("%s returned HTTP status %s: %q", c.taskURL, resp.Status, body)
|
||||
}
|
||||
|
||||
task, err := unmarshalTask(resp.Body)
|
||||
if err != nil {
|
||||
@@ -71,11 +80,18 @@ func (c *EcsClient) ContainerStats() (map[string]types.StatsJSON, error) {
|
||||
|
||||
req, _ := http.NewRequest("GET", c.statsURL, nil)
|
||||
resp, err := c.client.Do(req)
|
||||
|
||||
if err != nil {
|
||||
return map[string]types.StatsJSON{}, err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
// ignore the err here; LimitReader returns io.EOF and we're not interested in read errors.
|
||||
body, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 200))
|
||||
return nil, fmt.Errorf("%s returned HTTP status %s: %q", c.statsURL, resp.Status, body)
|
||||
}
|
||||
|
||||
statsMap, err := unmarshalStats(resp.Body)
|
||||
if err != nil {
|
||||
return map[string]types.StatsJSON{}, err
|
||||
|
||||
@@ -107,7 +107,8 @@ func TestEcsClient_Task(t *testing.T) {
|
||||
client: mockDo{
|
||||
do: func(req *http.Request) (*http.Response, error) {
|
||||
return &http.Response{
|
||||
Body: ioutil.NopCloser(rc),
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(rc),
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
@@ -123,11 +124,24 @@ func TestEcsClient_Task(t *testing.T) {
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "malformed resp",
|
||||
name: "malformed 500 resp",
|
||||
client: mockDo{
|
||||
do: func(req *http.Request) (*http.Response, error) {
|
||||
return &http.Response{
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte("foo"))),
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte("foo"))),
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "malformed 200 resp",
|
||||
client: mockDo{
|
||||
do: func(req *http.Request) (*http.Response, error) {
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte("foo"))),
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
@@ -164,7 +178,8 @@ func TestEcsClient_ContainerStats(t *testing.T) {
|
||||
client: mockDo{
|
||||
do: func(req *http.Request) (*http.Response, error) {
|
||||
return &http.Response{
|
||||
Body: ioutil.NopCloser(rc),
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(rc),
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
@@ -181,17 +196,31 @@ func TestEcsClient_ContainerStats(t *testing.T) {
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "malformed resp",
|
||||
name: "malformed 200 resp",
|
||||
client: mockDo{
|
||||
do: func(req *http.Request) (*http.Response, error) {
|
||||
return &http.Response{
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte("foo"))),
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte("foo"))),
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
want: map[string]types.StatsJSON{},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "malformed 500 resp",
|
||||
client: mockDo{
|
||||
do: func(req *http.Request) (*http.Response, error) {
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte("foo"))),
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
||||
Reference in New Issue
Block a user