From 28a26d2caeb4b0f1fe7c4b426cd6d6a82f32cad5 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 20 Feb 2020 20:24:31 +0300 Subject: [PATCH] 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. --- pkg/rpc/client/client.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pkg/rpc/client/client.go b/pkg/rpc/client/client.go index d41c6679a..58e1e95ad 100644 --- a/pkg/rpc/client/client.go +++ b/pkg/rpc/client/client.go @@ -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.