Skip to content

Commit

Permalink
fix: read error message when get a 500 error
Browse files Browse the repository at this point in the history
  • Loading branch information
minchao committed Apr 27, 2024
1 parent abfc56d commit 37a0aac
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
7 changes: 3 additions & 4 deletions cwb/cwb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion cwb/cwb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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)

Check failure on line 100 in cwb/cwb_test.go

View workflow job for this annotation

GitHub Actions / test (1.15.x)

undefined: io.ReadAll
if got, want := string(body), outBody; got != want {
t.Errorf("NewRequest(%q) Body is %v, want %v", inBody, got, want)
}
Expand Down

0 comments on commit 37a0aac

Please sign in to comment.