diff options
author | Martin Holst Swende <martin@swende.se> | 2018-04-19 21:32:43 +0800 |
---|---|---|
committer | Felix Lange <fjl@users.noreply.github.com> | 2018-04-19 21:32:43 +0800 |
commit | 8feb31825e50753bb63193c54fc13fda7655c062 (patch) | |
tree | 25560a1197f4c1070559a64d8bf99ce2302edea3 /rpc | |
parent | 8f8774cf6dd5bbcba9fa3773fa34d13d6523a147 (diff) | |
download | dexon-8feb31825e50753bb63193c54fc13fda7655c062.tar dexon-8feb31825e50753bb63193c54fc13fda7655c062.tar.gz dexon-8feb31825e50753bb63193c54fc13fda7655c062.tar.bz2 dexon-8feb31825e50753bb63193c54fc13fda7655c062.tar.lz dexon-8feb31825e50753bb63193c54fc13fda7655c062.tar.xz dexon-8feb31825e50753bb63193c54fc13fda7655c062.tar.zst dexon-8feb31825e50753bb63193c54fc13fda7655c062.zip |
rpc: handle HTTP response error codes (#16500)
Diffstat (limited to 'rpc')
-rw-r--r-- | rpc/http.go | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/rpc/http.go b/rpc/http.go index 14b6c1ab4..0c2e060fb 100644 --- a/rpc/http.go +++ b/rpc/http.go @@ -90,10 +90,19 @@ func DialHTTP(endpoint string) (*Client, error) { func (c *Client) sendHTTP(ctx context.Context, op *requestOp, msg interface{}) error { hc := c.writeConn.(*httpConn) respBody, err := hc.doRequest(ctx, msg) + if respBody != nil { + defer respBody.Close() + } + if err != nil { + if respBody != nil { + buf := new(bytes.Buffer) + if _, err2 := buf.ReadFrom(respBody); err2 == nil { + return fmt.Errorf("%v %v", err, buf.String()) + } + } return err } - defer respBody.Close() var respmsg jsonrpcMessage if err := json.NewDecoder(respBody).Decode(&respmsg); err != nil { return err @@ -132,6 +141,9 @@ func (hc *httpConn) doRequest(ctx context.Context, msg interface{}) (io.ReadClos if err != nil { return nil, err } + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + return resp.Body, errors.New(resp.Status) + } return resp.Body, nil } |