Add context to errors returned by bitcoinrpc

This commit is contained in:
Martin Boehm 2018-03-01 11:06:10 +01:00
parent 39675d4eed
commit 34400f5b2b
5 changed files with 26 additions and 22 deletions

View File

@ -56,6 +56,7 @@ go get github.com/gorilla/handlers
go get github.com/gorilla/mux
go get github.com/pebbe/zmq4
go get github.com/pkg/profile
go get github.com/juju/errors
```
Install blockbook:

View File

@ -9,6 +9,7 @@ import (
"time"
"github.com/golang/glog"
"github.com/juju/errors"
)
type RPCError struct {
@ -239,10 +240,10 @@ func (b *BitcoinRPC) GetBlockHash(height uint32) (string, error) {
err := b.call(&req, &res)
if err != nil {
return "", err
return "", errors.Annotatef(err, "height %v", height)
}
if res.Error != nil {
return "", res.Error
return "", errors.Annotatef(res.Error, "height %v", height)
}
return res.Result, nil
}
@ -258,10 +259,10 @@ func (b *BitcoinRPC) GetBlockHeader(hash string) (*BlockHeader, error) {
err := b.call(&req, &res)
if err != nil {
return nil, err
return nil, errors.Annotatef(err, "hash %v", hash)
}
if res.Error != nil {
return nil, res.Error
return nil, errors.Annotatef(res.Error, "hash %v", hash)
}
return &res.Result, nil
}
@ -281,7 +282,7 @@ func (b *BitcoinRPC) GetBlock(hash string) (*Block, error) {
}
block, err := b.Parser.ParseBlock(data)
if err != nil {
return nil, err
return nil, errors.Annotatef(err, "hash %v", hash)
}
block.BlockHeader = *header
return block, nil
@ -299,7 +300,7 @@ func (b *BitcoinRPC) GetBlockWithoutHeader(hash string, height uint32) (*Block,
}
block, err := b.Parser.ParseBlock(data)
if err != nil {
return nil, err
return nil, errors.Annotatef(err, "%v %v", height, hash)
}
block.BlockHeader.Hash = hash
block.BlockHeader.Height = height
@ -317,10 +318,10 @@ func (b *BitcoinRPC) GetBlockRaw(hash string) ([]byte, error) {
err := b.call(&req, &res)
if err != nil {
return nil, err
return nil, errors.Annotatef(err, "hash %v", hash)
}
if res.Error != nil {
return nil, res.Error
return nil, errors.Annotatef(res.Error, "hash %v", hash)
}
return hex.DecodeString(res.Result)
}
@ -337,10 +338,10 @@ func (b *BitcoinRPC) GetBlockList(hash string) (*Block, error) {
err := b.call(&req, &res)
if err != nil {
return nil, err
return nil, errors.Annotatef(err, "hash %v", hash)
}
if res.Error != nil {
return nil, res.Error
return nil, errors.Annotatef(res.Error, "hash %v", hash)
}
txs := make([]Tx, len(res.Result.Txids))
@ -369,10 +370,10 @@ func (b *BitcoinRPC) GetBlockFull(hash string) (*Block, error) {
err := b.call(&req, &res)
if err != nil {
return nil, err
return nil, errors.Annotatef(err, "hash %v", hash)
}
if res.Error != nil {
return nil, res.Error
return nil, errors.Annotatef(res.Error, "hash %v", hash)
}
return &res.Result, nil
}
@ -405,10 +406,10 @@ func (b *BitcoinRPC) GetTransaction(txid string) (*Tx, error) {
err := b.call(&req, &res)
if err != nil {
return nil, err
return nil, errors.Annotatef(err, "txid %v", txid)
}
if res.Error != nil {
return nil, res.Error
return nil, errors.Annotatef(res.Error, "txid %v", txid)
}
return &res.Result, nil
}

View File

@ -3,15 +3,15 @@ package main
import (
"context"
"encoding/hex"
"errors"
"flag"
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/juju/errors"
"blockbook/bchain"
"blockbook/db"
"blockbook/server"
@ -500,7 +500,7 @@ func connectBlocksParallel(
for {
block, err = chain.GetBlockWithoutHeader(hh.hash, hh.height)
if err != nil {
glog.Error("Connect block ", hh.height, " ", hh.hash, " error ", err, ". Retrying...")
glog.Error("Connect block error ", err, ". Retrying...")
time.Sleep(time.Millisecond * 500)
} else {
break
@ -540,12 +540,12 @@ ConnectLoop:
}
break
}
err = errors.New(fmt.Sprint("connectBlocksParallel interrupted at height ", h))
err = errors.Errorf("connectBlocksParallel interrupted at height %d", h)
break ConnectLoop
default:
hash, err = chain.GetBlockHash(h)
if err != nil {
glog.Error("GetBlockHash ", h, " error ", err)
glog.Error("GetBlockHash error ", err)
time.Sleep(time.Millisecond * 500)
continue
}

View File

@ -5,10 +5,11 @@ import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"os"
"path/filepath"
"github.com/juju/errors"
"github.com/bsm/go-vlq"
"github.com/golang/glog"

View File

@ -5,11 +5,12 @@ import (
"blockbook/db"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"github.com/juju/errors"
"github.com/golang/glog"
"github.com/martinboehm/golang-socketio"
"github.com/martinboehm/golang-socketio/transport"
@ -193,7 +194,7 @@ func (s *SocketIoServer) onMessage(c *gosocketio.Channel, req map[string]json.Ra
glog.V(1).Info(c.Id(), " onMessage ", method, " success")
return rv
}
glog.Error(c.Id(), " onMessage ", method, ": ", err)
glog.Error(c.Id(), " onMessage ", method, ": ", errors.ErrorStack(err))
e := resultError{}
e.Error.Message = err.Error()
return e