rpc/client: look into data first, then HTTP error code

In case of error our own server responds with an HTTP error and proper
JSON-RPC error in the body, so look there first as it has more specific data.
This commit is contained in:
Roman Khimov 2020-02-20 20:24:31 +03:00
parent 877b987ecf
commit 28a26d2cae

View file

@ -203,11 +203,14 @@ func (c *Client) performRequest(method string, p request.RawParams, v interface{
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("remote responded with a non 200 response: %d", resp.StatusCode)
// The node might send us proper JSON anyway, so look there first and if
// it parses, then it has more relevant data than HTTP error code.
err = json.NewDecoder(resp.Body).Decode(v)
if resp.StatusCode != http.StatusOK && err != nil {
err = fmt.Errorf("HTTP %d/%s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
return json.NewDecoder(resp.Body).Decode(v)
return err
}
// Ping attempts to create a connection to the endpoint.