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:
		@@ -744,23 +744,11 @@ func (r *Request) Stream() (io.ReadCloser, error) {
 | 
				
			|||||||
		// ensure we close the body before returning the error
 | 
							// ensure we close the body before returning the error
 | 
				
			||||||
		defer resp.Body.Close()
 | 
							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
 | 
							result := r.transformResponse(resp, req)
 | 
				
			||||||
		bodyBytes, err := ioutil.ReadAll(resp.Body)
 | 
							if result.err != nil {
 | 
				
			||||||
		if err != nil {
 | 
								return nil, result.err
 | 
				
			||||||
			return nil, fmt.Errorf("%v while accessing %v", resp.Status, url)
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("%d while accessing %v: %s", result.statusCode, url, string(result.body))
 | 
				
			||||||
		// 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)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -196,8 +196,10 @@ func StandardErrorMessage(err error) (string, bool) {
 | 
				
			|||||||
	switch {
 | 
						switch {
 | 
				
			||||||
	case isStatus:
 | 
						case isStatus:
 | 
				
			||||||
		switch s := status.Status(); {
 | 
							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
 | 
								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:
 | 
							default:
 | 
				
			||||||
			return fmt.Sprintf("Error from server: %s", err.Error()), true
 | 
								return fmt.Sprintf("Error from server: %s", err.Error()), true
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user