Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: unify error data struct #140

Merged
merged 2 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@ func (err *APIError) Error() string {
return fmt.Sprintf("API request failed: %s", err.Message)
}

func extractErrorMessage(r io.Reader) (errorMessage string) {
func extractErrorMessage(r io.Reader) (errorMessage string, err error) {
bs, err := ioutil.ReadAll(r)
if err != nil {
return
return "", err
}
var data struct{ Error struct{ Message string } }
err = json.Unmarshal(bs, &data)
if err == nil {
errorMessage = data.Error.Message
return data.Error.Message, nil
} else {
var data struct{ Error string }
json.Unmarshal(bs, &data)
errorMessage = data.Error
return "", err
}
return
}
10 changes: 5 additions & 5 deletions hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,8 @@ func TestRetireHost_NotFound(t *testing.T) {
t.Error("request method should be POST but: ", req.Method)
}

respJSON, _ := json.Marshal(map[string]string{
"error": "Host Not Found.",
respJSON, _ := json.Marshal(map[string]map[string]string{
"error": {"message": "Host Not Found."},
})

res.Header()["Content-Type"] = []string{"application/json"}
Expand Down Expand Up @@ -544,8 +544,8 @@ func TestBulkRetireHosts_NotFound(t *testing.T) {
t.Errorf("request IDs should be %+v but: %+v", expectIDs, data.IDs)
}

respJSON, _ := json.Marshal(map[string]string{
"error": "Hosts not found.",
respJSON, _ := json.Marshal(map[string]map[string]string{
"error": {"message": "Hosts Not Found."},
})

res.Header()["Content-Type"] = []string{"application/json"}
Expand All @@ -569,7 +569,7 @@ func TestBulkRetireHosts_NotFound(t *testing.T) {
if expectStatus := http.StatusNotFound; apiErr.StatusCode != expectStatus {
t.Errorf("api error StatusCode should be %d but got %d", expectStatus, apiErr.StatusCode)
}
if expect := "API request failed: Hosts not found."; apiErr.Error() != expect {
if expect := "API request failed: Hosts Not Found."; apiErr.Error() != expect {
t.Errorf("api error string should be \"%s\" but got \"%s\"", expect, apiErr.Error())
}
}
Expand Down
8 changes: 4 additions & 4 deletions mackerel.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ func (c *Client) Request(req *http.Request) (resp *http.Response, err error) {
}
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
message := extractErrorMessage(resp.Body)
message, err := extractErrorMessage(resp.Body)
defer resp.Body.Close()
if message != "" {
return nil, &APIError{StatusCode: resp.StatusCode, Message: message}
if err != nil {
return nil, &APIError{StatusCode: resp.StatusCode, Message: resp.Status}
}
return nil, &APIError{StatusCode: resp.StatusCode, Message: resp.Status}
return nil, &APIError{StatusCode: resp.StatusCode, Message: message}
}
return resp, nil
}
Expand Down