Check if error is Status in result.Stream()

Signed-off-by: Jess Frazelle <acidburn@google.com>
This commit is contained in:
Jess Frazelle 2017-01-12 14:52:25 -08:00
parent e1fa1512e4
commit 502a34e8dc
No known key found for this signature in database
GPG Key ID: 18F3685C0022BFF3
2 changed files with 31 additions and 3 deletions

View File

@ -760,10 +760,11 @@ func (r *Request) Stream() (io.ReadCloser, error) {
defer resp.Body.Close()
result := r.transformResponse(resp, req)
if result.err != nil {
return nil, result.err
err := result.Error()
if err == nil {
err = fmt.Errorf("%d while accessing %v: %s", result.statusCode, url, string(result.body))
}
return nil, fmt.Errorf("%d while accessing %v: %s", result.statusCode, url, string(result.body))
return nil, err
}
}

View File

@ -868,6 +868,7 @@ func TestRequestStream(t *testing.T) {
testCases := []struct {
Request *Request
Err bool
ErrFn func(error) bool
}{
{
Request: &Request{err: errors.New("bail")},
@ -903,6 +904,26 @@ func TestRequestStream(t *testing.T) {
},
Err: true,
},
{
Request: &Request{
client: clientFunc(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusBadRequest,
Body: ioutil.NopCloser(bytes.NewReader([]byte(`{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"a container name must be specified for pod kube-dns-v20-mz5cv, choose one of: [kubedns dnsmasq healthz]","reason":"BadRequest","code":400}`))),
}, nil
}),
content: defaultContentConfig(),
serializers: defaultSerializers(),
baseURL: &url.URL{},
},
Err: true,
ErrFn: func(err error) bool {
if err.Error() == "a container name must be specified for pod kube-dns-v20-mz5cv, choose one of: [kubedns dnsmasq healthz]" {
return true
}
return false
},
},
}
for i, testCase := range testCases {
testCase.Request.backoffMgr = &NoBackoff{}
@ -914,6 +935,12 @@ func TestRequestStream(t *testing.T) {
if hasErr && body != nil {
t.Errorf("%d: body should be nil when error is returned", i)
}
if hasErr {
if testCase.ErrFn != nil && !testCase.ErrFn(err) {
t.Errorf("unexpected error: %v", err)
}
}
}
}