Skip to content

Commit

Permalink
rpc: handle rpc (client) response error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Apr 18, 2018
1 parent 52b046c commit 5ab1be9
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion rpc/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 5ab1be9

Please sign in to comment.