Skip to content

Commit

Permalink
handling empty body - just continue with current behaviour and echo s…
Browse files Browse the repository at this point in the history
…tatus code only
  • Loading branch information
theflyingcodr committed Apr 9, 2021
1 parent b301d1a commit e8600e5
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,28 @@ func httpRequest(ctx context.Context, client *Client,
response.BodyContents, response.Error = ioutil.ReadAll(resp.Body)
}
// Check status code
if http.StatusOK != resp.StatusCode {
errBody := struct{
Error string `json:"error"`
}{}
if err := json.Unmarshal(response.BodyContents, &errBody); err != nil{
response.Error = fmt.Errorf("failed to unmarshal mapi error response: %w",err)
}
if http.StatusOK == resp.StatusCode {
return
}
// unexpected status, write an error.
if response.BodyContents == nil {
// no body so just echo status code.
response.Error = fmt.Errorf(
"status code: %d does not match %d, error: %s",
resp.StatusCode, http.StatusOK, errBody.Error,
"status code: %d does not match %d",
resp.StatusCode, http.StatusOK,
)
return
}
// have a body so map to an error type and add to the error message.
errBody := struct {
Error string `json:"error"`
}{}
if err := json.Unmarshal(response.BodyContents, &errBody); err != nil {
response.Error = fmt.Errorf("failed to unmarshal mapi error response: %w", err)
}
response.Error = fmt.Errorf(
"status code: %d does not match %d, error: %s",
resp.StatusCode, http.StatusOK, errBody.Error,
)
return
}

0 comments on commit e8600e5

Please sign in to comment.