From 164df4e70832b4a193056f8f609a53102f427a03 Mon Sep 17 00:00:00 2001 From: ckt1031 <65409152+ckt1031@users.noreply.github.com> Date: Fri, 14 Jul 2023 20:21:25 +0800 Subject: [PATCH] fix: resp body when error --- controller/channel-test.go | 12 ++++++++---- controller/relay-text.go | 14 ++++++-------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/controller/channel-test.go b/controller/channel-test.go index 39aec6ba..3f3ff2b5 100644 --- a/controller/channel-test.go +++ b/controller/channel-test.go @@ -55,10 +55,14 @@ func testChannel(channel *model.Channel, request ChatRequest) error { } if resp.StatusCode != http.StatusOK { - // Prinnt the body in string - buf := new(bytes.Buffer) - buf.ReadFrom(resp.Body) - return errors.New("error response: " + strconv.Itoa(resp.StatusCode) + " " + buf.String()) + // Print the body in string + if resp.Body != nil { + buf := new(bytes.Buffer) + buf.ReadFrom(resp.Body) + return errors.New("error response: " + strconv.Itoa(resp.StatusCode) + " " + buf.String()) + } + + return errors.New("error response: " + strconv.Itoa(resp.StatusCode)) } var done = false diff --git a/controller/relay-text.go b/controller/relay-text.go index fdbd6660..363c7f4d 100644 --- a/controller/relay-text.go +++ b/controller/relay-text.go @@ -181,16 +181,14 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { return errorWrapper(err, "do_request_failed", http.StatusInternalServerError) } if resp.StatusCode != http.StatusOK { - // Print Data if Error - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return errorWrapper(err, "read_response_body_failed", http.StatusInternalServerError) + // Print the body in string + if resp.Body != nil { + buf := new(bytes.Buffer) + buf.ReadFrom(resp.Body) + log.Printf("Error Channel (%s): %s", baseURL, buf.String()) + return errorWrapper(err, "request_failed", resp.StatusCode) } - bodyString := string(bodyBytes) - - log.Printf("Error: %s", bodyString) - return errorWrapper(err, "request_failed", resp.StatusCode) } err = req.Body.Close()