Skip to content

Commit

Permalink
Refactor errors to improve overall consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuasing committed Apr 16, 2024
1 parent 88047e7 commit fc75ae4
Show file tree
Hide file tree
Showing 36 changed files with 267 additions and 253 deletions.
5 changes: 3 additions & 2 deletions api/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"crypto/rand"
"crypto/sha256"
"errors"
"fmt"
"io"

Expand Down Expand Up @@ -54,13 +55,13 @@ func MustNewAuthenticateMessage(message string) *AuthenticateMessage {

func NewAuthenticateFromBytes(b []byte) (*AuthenticateMessage, error) {
if len(b) < nonceLength {
return nil, fmt.Errorf("authenicate message too short")
return nil, errors.New("authenicate message too short")
}
am := &AuthenticateMessage{}
copy(am.Nonce[0:], b[:nonceLength])
am.Message = string(b[nonceLength:])
if bytes.Equal(am.Nonce[:], zeroNonce[:]) {
return nil, fmt.Errorf("invalid nonce")
return nil, errors.New("invalid nonce")
}
return am, nil
}
Expand Down
5 changes: 3 additions & 2 deletions api/auth/secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package auth
import (
"context"
"encoding/hex"
"errors"
"fmt"
"reflect"

Expand Down Expand Up @@ -215,7 +216,7 @@ func (s *Secp256k1Auth) HandshakeServer(ctx context.Context, conn protocol.APICo
return fmt.Errorf("hello challenge accepted unexpected state: %v", state)
}
if am == nil {
return fmt.Errorf("hello challenge accepted message not set")
return errors.New("hello challenge accepted message not set")
}

derived, err := handleSecp256k1HelloChallengeAccepted(am, c)
Expand All @@ -225,7 +226,7 @@ func (s *Secp256k1Auth) HandshakeServer(ctx context.Context, conn protocol.APICo

// Exit state machine
if !derived.IsEqual(s.pubKey) {
return fmt.Errorf("handleSecp256k1HelloChallengeAccepted: not the same signer")
return errors.New("handleSecp256k1HelloChallengeAccepted: not the same signer")
}
s.remotePubKey = derived
log.Tracef("HandshakeServer complete: %x",
Expand Down
2 changes: 1 addition & 1 deletion api/protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func (ac *Conn) Connect(ctx context.Context) error {
log.Tracef("Connect: dialing %v", ac.serverURL)
conn, _, err := websocket.Dial(connectCtx, ac.serverURL, nil)
if err != nil {
return fmt.Errorf("failed to dial server: %w", err)
return fmt.Errorf("dial server: %w", err)
}
conn.SetReadLimit(512 * 1024) // XXX - default is 32KB
defer func() {
Expand Down
8 changes: 4 additions & 4 deletions bitcoin/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (bh *BlockHeader) String() string {

func RawBlockHeaderFromSlice(s []byte) (*BlockHeader, error) {
if len(s) != 80 {
return nil, fmt.Errorf("invalid blockheader size")
return nil, errors.New("invalid blockheader size")
}
var bh BlockHeader
copy(bh[:], s)
Expand Down Expand Up @@ -117,14 +117,14 @@ func SignTx(btx *wire.MsgTx, payToScript []byte, privateKey *dcrsecp256k1.Privat
txscript.SigHashAll, btx, 0,
)
if err != nil {
return fmt.Errorf("failed to calculate signature hash: %w", err)
return fmt.Errorf("calculate signature hash: %w", err)
}
pubKeyBytes := publicKey.SerializeCompressed()
sig := dcrecdsa.Sign(privateKey, sigHash)
sigBytes := append(sig.Serialize(), byte(txscript.SigHashAll))
sb := txscript.NewScriptBuilder().AddData(sigBytes).AddData(pubKeyBytes)
if btx.TxIn[0].SignatureScript, err = sb.Script(); err != nil {
return fmt.Errorf("failed to build signature script: %w", err)
return fmt.Errorf("build signature script: %w", err)
}
return nil
}
Expand Down Expand Up @@ -157,7 +157,7 @@ func KeysAndAddressFromHexString(s string, chainParams *chaincfg.Params) (*dcrse
btcAddress, err := btcutil.NewAddressPubKey(pubKeyBytes, chainParams)
if err != nil {
return nil, nil, nil,
fmt.Errorf("failed to create BTC address from public key: %w", err)
fmt.Errorf("create BTC address from public key: %w", err)
}

return privKey, privKey.PubKey(), btcAddress.AddressPubKeyHash(), nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/bfgd/bfgd.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ func _main() error {

server, err := bfg.NewServer(cfg)
if err != nil {
return fmt.Errorf("failed to create BFG server: %w", err)
return fmt.Errorf("create BFG server: %w", err)
}
if err = server.Run(ctx); !errors.Is(err, context.Canceled) {
return fmt.Errorf("BFG server terminated: %w", err)
return fmt.Errorf("bfg server terminated: %w", err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/bssd/bssd.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ func _main() error {

server, err := bss.NewServer(cfg)
if err != nil {
return fmt.Errorf("failed to create BSS server: %w", err)
return fmt.Errorf("create BSS server: %w", err)
}
if err := server.Run(ctx); !errors.Is(err, context.Canceled) {
return fmt.Errorf("BSS server terminated with error: %w", err)
return fmt.Errorf("bss server terminated: %w", err)
}

return nil
Expand Down
22 changes: 11 additions & 11 deletions cmd/btctool/bdf/bdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ func Header2Hex(wbh *wire.BlockHeader) (string, error) {
func Hex2Header(header string) (*wire.BlockHeader, error) {
blockHeader, err := hex.DecodeString(header)
if err != nil {
return nil, fmt.Errorf("DecodeString: %v", err)
return nil, fmt.Errorf("decode string: %w", err)
}
var bh wire.BlockHeader
err = bh.Deserialize(bytes.NewReader(blockHeader))
if err != nil {
return nil, fmt.Errorf("Deserialize: %v", err)
return nil, fmt.Errorf("deserialize block header: %w", err)
}
return &bh, nil
}
Expand All @@ -155,14 +155,14 @@ func writeHeight(height int, hash, dir string) error {
// do nothing
defer f.Close()
} else {
return fmt.Errorf("Open: %v", err)
return fmt.Errorf("open file: %w", err)
}
} else {
defer f.Close()
d := json.NewDecoder(f)
err = d.Decode(&lh)
if err != nil {
return fmt.Errorf("%v corrupt: %v", filename, err)
return fmt.Errorf("%v corrupt: %w", filename, err)
}
}
if lh.Height > height {
Expand All @@ -171,14 +171,14 @@ func writeHeight(height int, hash, dir string) error {
}
fw, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
if err != nil {
return fmt.Errorf("OpenFile: %v", err)
return fmt.Errorf("open file: %w", err)
}
e := json.NewEncoder(fw)
lh.Height = height
lh.Hash = hash
err = e.Encode(lh)
if err != nil {
return fmt.Errorf("Encode: %v", err)
return fmt.Errorf("encode: %w", err)
}
return fw.Close()
}
Expand All @@ -195,7 +195,7 @@ func writeHeader(height int, hash, header, dir string) error {
}
f, err := os.Create(filename)
if err != nil {
return fmt.Errorf("Create: %v", err)
return fmt.Errorf("create file: %w", err)
}
e := json.NewEncoder(f)
err = e.Encode(Header{
Expand All @@ -204,7 +204,7 @@ func writeHeader(height int, hash, header, dir string) error {
})
if err != nil {
f.Close()
return fmt.Errorf("Encode: %v", err)
return fmt.Errorf("encode json: %w", err)
}

return f.Close()
Expand All @@ -213,19 +213,19 @@ func writeHeader(height int, hash, header, dir string) error {
func WriteHeader(height int, header, dir string) error {
bh, err := Hex2Header(header)
if err != nil {
return fmt.Errorf("Hex2Header: %v", err)
return fmt.Errorf("convert hex to header: %w", err)
}

dfm.Lock()
defer dfm.Unlock()

err = writeHeader(height, bh.BlockHash().String(), header, dir)
if err != nil {
return fmt.Errorf("writeHeader: %v", err)
return fmt.Errorf("write header: %w", err)
}
err = writeHeight(height, bh.BlockHash().String(), dir)
if err != nil {
return fmt.Errorf("writeHeight: %v", err)
return fmt.Errorf("write height: %w", err)
}
return nil
}
16 changes: 8 additions & 8 deletions cmd/btctool/blockstream/blockstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ type TBlock struct {
func Tip(ctx context.Context) (int, error) {
b, err := httpclient.Request(ctx, "GET", bsURL+"/blocks/tip/height", nil)
if err != nil {
return 0, fmt.Errorf("request: %v", err)
return 0, fmt.Errorf("request: %w", err)
}
height, err := strconv.ParseInt(string(b), 10, 64)
if err != nil {
return 0, fmt.Errorf("ParseUint: %v", err)
return 0, fmt.Errorf("parse uint: %w", err)
}

return int(height), nil
Expand All @@ -51,23 +51,23 @@ func Tip(ctx context.Context) (int, error) {
func BlockHeader(ctx context.Context, hash string) (string, error) {
bh, err := httpclient.Request(ctx, "GET", bsURL+"/block/"+hash+"/header", nil)
if err != nil {
return "", fmt.Errorf("request: %v", err)
return "", fmt.Errorf("request: %w", err)
}
_, err = hex.DecodeString(string(bh))
if err != nil {
return "", fmt.Errorf("DecodeString: %v", err)
return "", fmt.Errorf("decode hex: %w", err)
}
return string(bh), nil
}

func BlockHeightHash(ctx context.Context, height string) (string, error) {
bh, err := httpclient.Request(ctx, "GET", bsURL+"/block-height/"+height, nil)
if err != nil {
return "", fmt.Errorf("request: %v", err)
return "", fmt.Errorf("request: %w", err)
}
_, err = hex.DecodeString(string(bh))
if err != nil {
return "", fmt.Errorf("DecodeString: %v", err)
return "", fmt.Errorf("decode hex: %w", err)
}
return string(bh), nil
}
Expand All @@ -79,7 +79,7 @@ func Block(ctx context.Context, hash string, raw bool) (string, error) {
}
b, err := httpclient.Request(ctx, "GET", bsURL+"/block/"+hash+suffix, nil)
if err != nil {
return "", fmt.Errorf("request: %v", err)
return "", fmt.Errorf("request: %w", err)
}
if raw {
return hex.EncodeToString(b), nil
Expand All @@ -91,7 +91,7 @@ func BlockBytes(ctx context.Context, hash string) ([]byte, error) {
suffix := "/raw"
b, err := httpclient.Request(ctx, "GET", bsURL+"/block/"+hash+suffix, nil)
if err != nil {
return nil, fmt.Errorf("request: %v", err)
return nil, fmt.Errorf("request: %w", err)
}
return b, nil
}
Loading

0 comments on commit fc75ae4

Please sign in to comment.