Skip to content

Commit

Permalink
Add comments to explain error handling logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
jholdstock committed Nov 22, 2022
1 parent ca20982 commit 3d70faf
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,23 @@ func (c *Client) do(ctx context.Context, method, path string, addr stdaddr.Addre
status := reply.StatusCode

if status != http.StatusOK {
// If no response body, just return status.
// If no response body, return an error with just the HTTP status.
if len(respBody) == 0 {
return fmt.Errorf("http status %d (%s) with no body",
status, http.StatusText(status))
}

// Try unmarshal response body to a known vspd error.
// Try to unmarshal the response body to a known vspd error.
var apiError types.ErrorResponse
err = json.Unmarshal(respBody, &apiError)
if err == nil {
return apiError
}

// If the response body could not be unmarshalled it might not have come
// from vspd (eg. it could be from an nginx reverse proxy or some other
// intermediary server). Return an error with the HTTP status and the
// full body so that it may be investigated.
return fmt.Errorf("http status %d (%s) with body %q",
status, http.StatusText(status), respBody)
}
Expand Down

0 comments on commit 3d70faf

Please sign in to comment.