Merge pull request #39831 from jessfraz/fix-38774

Automatic merge from submit-queue (batch tested with PRs 39772, 39831, 39481, 40167, 40149)

Check if error is Status in result.Stream()

Fix #38774

This adds the same functionality to `.Stream()` that was added to `.Error()`, `.Into()`, and `.Get()` in ce187f9c6a to try decoding the body as a Status.

This broke `.Stream()` because the decoding of the body as `Status` was removed from `transformResponse` in ce187f9c6a (diff-de85e3effc36b7bbe3fb9eae6c833cf3L933)
This commit is contained in:
Kubernetes Submit Queue 2017-01-19 17:56:45 -08:00 committed by GitHub
commit 4e3488ee99
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() defer resp.Body.Close()
result := r.transformResponse(resp, req) result := r.transformResponse(resp, req)
if result.err != nil { err := result.Error()
return nil, result.err 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 { testCases := []struct {
Request *Request Request *Request
Err bool Err bool
ErrFn func(error) bool
}{ }{
{ {
Request: &Request{err: errors.New("bail")}, Request: &Request{err: errors.New("bail")},
@ -903,6 +904,26 @@ func TestRequestStream(t *testing.T) {
}, },
Err: true, 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 { for i, testCase := range testCases {
testCase.Request.backoffMgr = &NoBackoff{} testCase.Request.backoffMgr = &NoBackoff{}
@ -914,6 +935,12 @@ func TestRequestStream(t *testing.T) {
if hasErr && body != nil { if hasErr && body != nil {
t.Errorf("%d: body should be nil when error is returned", i) 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)
}
}
} }
} }