Skip to content

Commit

Permalink
Add more debugging data for bad http responses (#638) (#704)
Browse files Browse the repository at this point in the history
* Add more debugging data for bad http responses

* add changelog

* Apply Mark's suggested fix

* move changelog entry folder

(cherry picked from commit 506942742b29d7a83b6abefca2282d6b56d7bf9c)

Co-authored-by: Dev Ojha <[email protected]>
  • Loading branch information
mergify[bot] and ValarDragon authored Apr 12, 2023
1 parent 302d18e commit 244d999
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- `[jsonrpc/client]` Improve the error message for client errors stemming from
bad HTTP responses.
([cometbft/cometbft\#638](https://github.com/cometbft/cometbft/pull/638))
13 changes: 10 additions & 3 deletions rpc/jsonrpc/client/http_json_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,22 @@ func (c *Client) Call(
if err != nil {
return nil, fmt.Errorf("post failed: %w", err)
}

defer httpResponse.Body.Close()

responseBytes, err := io.ReadAll(httpResponse.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
return nil, fmt.Errorf("%s. Failed to read response body: %w", getHTTPRespErrPrefix(httpResponse), err)
}

return unmarshalResponseBytes(responseBytes, id, result)
res, err := unmarshalResponseBytes(responseBytes, id, result)
if err != nil {
return nil, fmt.Errorf("%s. %w", getHTTPRespErrPrefix(httpResponse), err)
}
return res, nil
}

func getHTTPRespErrPrefix(resp *http.Response) string {
return fmt.Sprintf("error in json rpc client, with http response metadata: (Status: %s, Protocol %s)", resp.Status, resp.Proto)
}

// NewRequestBatch starts a batch of requests for this client.
Expand Down

0 comments on commit 244d999

Please sign in to comment.