Handle HTTP status codes returned from blockchain rpc
This commit is contained in:
parent
e914cd4479
commit
fbc5248ef8
@ -581,6 +581,15 @@ func (b *BitcoinRPC) call(req interface{}, res interface{}) error {
|
||||
// read the entire response body until the end to avoid memory leak when reusing http connection
|
||||
// see http://devs.cloudimmunity.com/gotchas-and-common-mistakes-in-go-golang/
|
||||
defer io.Copy(ioutil.Discard, httpRes.Body)
|
||||
// if server returns HTTP error code it might not return json with response
|
||||
// handle both cases
|
||||
if httpRes.StatusCode != 200 {
|
||||
err = json.NewDecoder(httpRes.Body).Decode(&res)
|
||||
if err != nil {
|
||||
return errors.New(httpRes.Status)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return json.NewDecoder(httpRes.Body).Decode(&res)
|
||||
}
|
||||
|
||||
|
||||
@ -111,12 +111,12 @@ func main() {
|
||||
|
||||
var err error
|
||||
if chain, err = coins.NewBlockChain(*coin, *rpcURL, *rpcUser, *rpcPass, time.Duration(*rpcTimeout)*time.Second, *parse); err != nil {
|
||||
glog.Fatal("NewBlockChain: ", err)
|
||||
glog.Fatal("rpc: ", err)
|
||||
}
|
||||
|
||||
index, err = db.NewRocksDB(*dbPath, chain.GetChainParser())
|
||||
if err != nil {
|
||||
glog.Fatal("NewRocksDB: ", err)
|
||||
glog.Fatal("rocksDB: ", err)
|
||||
}
|
||||
defer index.Close()
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user