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/gorilla/mux
go get github.com/pebbe/zmq4 go get github.com/pebbe/zmq4
go get github.com/pkg/profile go get github.com/pkg/profile
go get github.com/juju/errors
``` ```
Install blockbook: Install blockbook:

View File

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

View File

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

View File

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

View File

@ -5,11 +5,12 @@ import (
"blockbook/db" "blockbook/db"
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"strings" "strings"
"github.com/juju/errors"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/martinboehm/golang-socketio" "github.com/martinboehm/golang-socketio"
"github.com/martinboehm/golang-socketio/transport" "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") glog.V(1).Info(c.Id(), " onMessage ", method, " success")
return rv return rv
} }
glog.Error(c.Id(), " onMessage ", method, ": ", err) glog.Error(c.Id(), " onMessage ", method, ": ", errors.ErrorStack(err))
e := resultError{} e := resultError{}
e.Error.Message = err.Error() e.Error.Message = err.Error()
return e return e