From 3c0cca3e9cc74faf4c72055749c331de9b48dca7 Mon Sep 17 00:00:00 2001 From: Matthew Oliver Date: Fri, 5 Dec 2025 13:33:06 -0800 Subject: [PATCH] Improve error message for invalid HTTP response codes. Include the response body in the error message when an unexpected HTTP status code is received, to provide more context for debugging. PiperOrigin-RevId: 840855002 --- cli/cli.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index 0913755..c8f2087 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -99,15 +99,15 @@ func post(c client, msg interface{}, addr string) (*models.Response, error) { } defer res.Body.Close() - if res.StatusCode < http.StatusOK || res.StatusCode > http.StatusIMUsed { - return nil, fmt.Errorf("invalid response code received for request: %d", res.StatusCode) - } - respBody, err := ioutil.ReadAll(res.Body) if err != nil { return nil, fmt.Errorf("error reading response body: %v", err) } + if res.StatusCode < http.StatusOK || res.StatusCode > http.StatusIMUsed { + return nil, fmt.Errorf("invalid response code received for request: %d with body: %s", res.StatusCode, respBody) + } + resp := &models.Response{} if err := json.Unmarshal(respBody, resp); err != nil { msg := fmt.Sprintf("json.Unmarshal returned: %v\n\nResponse Body: %s", err, respBody)