Skip to content

Commit 8feb318

Browse files
holimanfjl
authored andcommitted
rpc: handle HTTP response error codes (#16500)
1 parent 8f8774c commit 8feb318

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

‎rpc/http.go‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,19 @@ func DialHTTP(endpoint string) (*Client, error) {
9090
func (c *Client) sendHTTP(ctx context.Context, op *requestOp, msg interface{}) error {
9191
hc := c.writeConn.(*httpConn)
9292
respBody, err := hc.doRequest(ctx, msg)
93+
if respBody != nil {
94+
defer respBody.Close()
95+
}
96+
9397
if err != nil {
98+
if respBody != nil {
99+
buf := new(bytes.Buffer)
100+
if _, err2 := buf.ReadFrom(respBody); err2 == nil {
101+
return fmt.Errorf("%v %v", err, buf.String())
102+
}
103+
}
94104
return err
95105
}
96-
defer respBody.Close()
97106
var respmsg jsonrpcMessage
98107
if err := json.NewDecoder(respBody).Decode(&respmsg); err != nil {
99108
return err
@@ -132,6 +141,9 @@ func (hc *httpConn) doRequest(ctx context.Context, msg interface{}) (io.ReadClos
132141
if err != nil {
133142
return nil, err
134143
}
144+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
145+
return resp.Body, errors.New(resp.Status)
146+
}
135147
return resp.Body, nil
136148
}
137149

0 commit comments

Comments
 (0)