From 37a0aac062e2c3a2a71310feaefb505d72adb167 Mon Sep 17 00:00:00 2001 From: minchao Date: Sat, 27 Apr 2024 15:13:35 +0800 Subject: [PATCH] fix: read error message when get a 500 error --- cwb/cwb.go | 7 +++---- cwb/cwb_test.go | 3 ++- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cwb/cwb.go b/cwb/cwb.go index 1eb568d..56d7765 100644 --- a/cwb/cwb.go +++ b/cwb/cwb.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "net/url" ) @@ -147,8 +146,8 @@ func checkResponse(r *http.Response) error { errorResponse := &ErrorResponse{Response: r, Message: "unknown"} switch r.StatusCode { - case http.StatusUnauthorized, http.StatusNotFound: - data, err := ioutil.ReadAll(r.Body) + case http.StatusUnauthorized, http.StatusNotFound, http.StatusInternalServerError: + data, err := io.ReadAll(r.Body) if err != nil { errorResponse.Message = fmt.Sprintf("reading body, %s", err.Error()) } else { @@ -160,7 +159,7 @@ func checkResponse(r *http.Response) error { // decodeResponse decodes the API response. func decodeResponse(body io.Reader, to interface{}) error { - data, err := ioutil.ReadAll(body) + data, err := io.ReadAll(body) if err != nil { return err } diff --git a/cwb/cwb_test.go b/cwb/cwb_test.go index b559c93..ca85de5 100644 --- a/cwb/cwb_test.go +++ b/cwb/cwb_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "io" "io/ioutil" "net/http" "net/http/httptest" @@ -96,7 +97,7 @@ func TestClient_NewRequest(t *testing.T) { t.Errorf("NewRequest(%q) URL is %v, want %v", inURL, got, want) } - body, _ := ioutil.ReadAll(req.Body) + body, _ := io.ReadAll(req.Body) if got, want := string(body), outBody; got != want { t.Errorf("NewRequest(%q) Body is %v, want %v", inBody, got, want) }