4
4
"crypto/tls"
5
5
"encoding/json"
6
6
"errors"
7
+ "fmt"
7
8
"io/ioutil"
8
9
"net/http"
9
10
"net/url"
@@ -61,21 +62,21 @@ func (c *Client) Run(method string, params interface{}, result interface{}, opts
61
62
return err
62
63
}
63
64
64
- jsonResponse , err := c .sendJSONRequest (jsonRequest , opts ... )
65
+ httpResponse , err := c .sendJSONRequest (jsonRequest , opts ... )
65
66
if err != nil {
66
67
return err
67
68
}
68
69
69
- response := NewResponse ()
70
- response .Result = result
70
+ jsonRPCResponse := NewResponse ()
71
+ jsonRPCResponse .Result = result
71
72
72
- err = json .Unmarshal (jsonResponse , & response )
73
+ err = json .Unmarshal (httpResponse , & jsonRPCResponse )
73
74
if err != nil {
74
75
return err
75
76
}
76
77
77
- if response .hasError () {
78
- return errors .New (response .Error .Message )
78
+ if jsonRPCResponse .hasError () {
79
+ return errors .New (jsonRPCResponse .Error .Message )
79
80
}
80
81
81
82
return nil
@@ -104,8 +105,6 @@ func (c *Client) Notify(method string, params interface{}, opts ...NotifyOptions
104
105
}
105
106
106
107
func (c * Client ) sendJSONRequest (jsonRequest []byte , opts ... RunOptions ) ([]byte , error ) {
107
- var jsonResponse []byte
108
-
109
108
httpRequest , err := http .NewRequest ("POST" , c .serverURL , strings .NewReader (string (jsonRequest )))
110
109
httpRequest .Header .Set ("Content-Type" , "application/json" )
111
110
httpRequest .Header .Set ("Content-Length" , "" )
@@ -121,15 +120,19 @@ func (c *Client) sendJSONRequest(jsonRequest []byte, opts ...RunOptions) ([]byte
121
120
122
121
httpResponse , err := c .httpClient .Do (httpRequest )
123
122
if err != nil {
124
- return jsonResponse , err
123
+ return nil , err
125
124
}
126
125
127
126
defer httpResponse .Body .Close ()
128
127
129
- jsonResponse , err = ioutil .ReadAll (httpResponse .Body )
128
+ httpResponseBody , err : = ioutil .ReadAll (httpResponse .Body )
130
129
if err != nil {
131
- return jsonResponse , err
130
+ return httpResponseBody , err
131
+ }
132
+
133
+ if httpResponse .StatusCode >= http .StatusBadRequest {
134
+ return nil , fmt .Errorf ("received HTTP status %d with body %s" , httpResponse .StatusCode , httpResponseBody )
132
135
}
133
136
134
- return jsonResponse , nil
137
+ return httpResponseBody , nil
135
138
}
0 commit comments