From 5e6f5fee8a5a75c0806eec6ff17e2344f88d73be Mon Sep 17 00:00:00 2001 From: Clayton Coleman Date: Sun, 4 Sep 2016 14:42:30 -0400 Subject: [PATCH] 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. --- pkg/client/restclient/request.go | 20 ++++---------------- pkg/kubectl/cmd/util/helpers.go | 4 +++- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/pkg/client/restclient/request.go b/pkg/client/restclient/request.go index b57f5558e42..4f168afa67d 100644 --- a/pkg/client/restclient/request.go +++ b/pkg/client/restclient/request.go @@ -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)) } } diff --git a/pkg/kubectl/cmd/util/helpers.go b/pkg/kubectl/cmd/util/helpers.go index dac0610e7ad..0b47a5a2dd9 100644 --- a/pkg/kubectl/cmd/util/helpers.go +++ b/pkg/kubectl/cmd/util/helpers.go @@ -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 }