Handle non successfull HTTP responses

This change set equips the RPC client with handling of non successful
HTTP responses. An HTTP response is considered non successful when its
status code is not in the range 200..299
This commit is contained in:
Tomás Senart 2014-07-03 14:04:58 +02:00
parent 0e463baf95
commit 77fdb1011b

View file

@ -614,22 +614,30 @@ func (c *Client) handleSendPostMessage(details *sendPostDetails) {
log.Tracef("Sending command [%s] with id %d", cmd.Method(), cmd.Id()) log.Tracef("Sending command [%s] with id %d", cmd.Method(), cmd.Id())
httpResponse, err := c.httpClient.Do(details.request) httpResponse, err := c.httpClient.Do(details.request)
if err != nil { if err != nil {
details.responseChan <- &response{result: nil, err: err} details.responseChan <- &response{err: err}
return return
} }
// Read the raw bytes and close the response. // Read the raw bytes and close the response.
respBytes, err := btcjson.GetRaw(httpResponse.Body) respBytes, err := btcjson.GetRaw(httpResponse.Body)
if err != nil { if err != nil {
details.responseChan <- &response{result: nil, err: err} details.responseChan <- &response{err: err}
return return
} }
// Handle unsuccessful HTTP responses
if httpResponse.StatusCode < 200 || httpResponse.StatusCode >= 300 {
details.responseChan <- &response{err: errors.New(string(respBytes))}
return
}
var resp rawResponse var resp rawResponse
err = json.Unmarshal(respBytes, &resp) err = json.Unmarshal(respBytes, &resp)
if err != nil { if err != nil {
details.responseChan <- &response{result: nil, err: err} details.responseChan <- &response{err: err}
return return
} }
res, err := resp.result() res, err := resp.result()
details.responseChan <- &response{result: res, err: err} details.responseChan <- &response{result: res, err: err}
} }