Handle HTTP status codes returned from blockchain rpc

This commit is contained in:
Martin Boehm 2018-03-11 01:31:09 +01:00
parent e914cd4479
commit fbc5248ef8
2 changed files with 11 additions and 2 deletions

View File

@ -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)
}

View File

@ -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()