Merge pull request #32048 from smarterclayton/consistent_errors

Automatic merge from submit-queue

Handle Stream() errors consistently in restclient

We should be following the same rules for Stream() as the normal body
request flow.

Also add slightly more output on a server error - in the future we may
want to clean this up but it's potentially hiding bad responses.

Related to #32009 but isn't the fix (so far)
This commit is contained in:
Kubernetes Submit Queue 2016-09-15 20:13:48 -07:00 committed by GitHub
commit 27c093ea90
2 changed files with 7 additions and 17 deletions

View File

@ -744,23 +744,11 @@ func (r *Request) Stream() (io.ReadCloser, error) {
// ensure we close the body before returning the error
defer resp.Body.Close()
// we have a decent shot at taking the object returned, parsing it as a status object and returning a more normal error
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("%v while accessing %v", resp.Status, url)
result := r.transformResponse(resp, req)
if result.err != nil {
return nil, result.err
}
// TODO: Check ContentType.
if runtimeObject, err := runtime.Decode(r.serializers.Decoder, bodyBytes); err == nil {
statusError := errors.FromObject(runtimeObject)
if _, ok := statusError.(errors.APIStatus); ok {
return nil, statusError
}
}
bodyText := string(bodyBytes)
return nil, fmt.Errorf("%s while accessing %v: %s", resp.Status, url, bodyText)
return nil, fmt.Errorf("%d while accessing %v: %s", result.statusCode, url, string(result.body))
}
}

View File

@ -196,8 +196,10 @@ func StandardErrorMessage(err error) (string, bool) {
switch {
case isStatus:
switch s := status.Status(); {
case s.Reason == "Unauthorized":
case s.Reason == unversioned.StatusReasonUnauthorized:
return fmt.Sprintf("error: You must be logged in to the server (%s)", s.Message), true
case len(s.Reason) > 0:
return fmt.Sprintf("Error from server (%s): %s", s.Reason, err.Error()), true
default:
return fmt.Sprintf("Error from server: %s", err.Error()), true
}