Skip to content

Commit

Permalink
multi: Wrap errors for better errors.Is/As support.
Browse files Browse the repository at this point in the history
This updates all remaining cases of unwrapped errors in fmt.Errorf calls
to wrap the underlying errors with the %w format verb to ensure they
work nicely with errors.Is and errors.As.
  • Loading branch information
davecgh committed Aug 25, 2023
1 parent ba7e40f commit 79be34a
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 69 deletions.
16 changes: 8 additions & 8 deletions addrmgr/addrmanager.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2013-2014 The btcsuite developers
// Copyright (c) 2015-2021 The Decred developers
// Copyright (c) 2015-2023 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -511,7 +511,7 @@ func (a *AddrManager) loadPeers() {

err := a.deserializePeers(a.peersFile)
if err != nil {
log.Errorf("Failed to parse file %s: %v", a.peersFile, err)
log.Errorf("Failed to parse file %s: %w", a.peersFile, err)
// if it is invalid we nuke the old one unconditionally.
err = os.Remove(a.peersFile)
if err != nil {
Expand All @@ -531,15 +531,15 @@ func (a *AddrManager) deserializePeers(filePath string) error {
}
r, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("%s error opening file: %v", filePath, err)
return fmt.Errorf("%s error opening file: %w", filePath, err)
}
defer r.Close()

var sam serializedAddrManager
dec := json.NewDecoder(r)
err = dec.Decode(&sam)
if err != nil {
return fmt.Errorf("error reading %s: %v", filePath, err)
return fmt.Errorf("error reading %s: %w", filePath, err)
}

if sam.Version != serialisationVersion {
Expand All @@ -551,13 +551,13 @@ func (a *AddrManager) deserializePeers(filePath string) error {
for _, v := range sam.Addresses {
netAddr, err := a.newAddressFromString(v.Addr)
if err != nil {
return fmt.Errorf("failed to deserialize netaddress "+
"%s: %v", v.Addr, err)
return fmt.Errorf("failed to deserialize netaddress %s: %w", v.Addr,
err)
}
srcAddr, err := a.newAddressFromString(v.Src)
if err != nil {
return fmt.Errorf("failed to deserialize netaddress "+
"%s: %v", v.Src, err)
return fmt.Errorf("failed to deserialize netaddress %s: %w", v.Src,
err)
}

ka := &KnownAddress{
Expand Down
12 changes: 6 additions & 6 deletions certgen/certgen.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2015-2017 The Decred developers
// Copyright (c) 2015-2023 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -45,7 +45,7 @@ func NewTLSCertPair(curve elliptic.Curve, organization string, validUntil time.T
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return nil, nil, fmt.Errorf("failed to generate serial number: %s", err)
return nil, nil, fmt.Errorf("failed to generate serial number: %w", err)
}

host, err := os.Hostname()
Expand Down Expand Up @@ -120,24 +120,24 @@ func NewTLSCertPair(curve elliptic.Curve, organization string, validUntil time.T
derBytes, err := x509.CreateCertificate(rand.Reader, &template,
&template, &priv.PublicKey, priv)
if err != nil {
return nil, nil, fmt.Errorf("failed to create certificate: %v", err)
return nil, nil, fmt.Errorf("failed to create certificate: %w", err)
}

certBuf := &bytes.Buffer{}
err = pem.Encode(certBuf, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
if err != nil {
return nil, nil, fmt.Errorf("failed to encode certificate: %v", err)
return nil, nil, fmt.Errorf("failed to encode certificate: %w", err)
}

keybytes, err := x509.MarshalECPrivateKey(priv)
if err != nil {
return nil, nil, fmt.Errorf("failed to marshal private key: %v", err)
return nil, nil, fmt.Errorf("failed to marshal private key: %w", err)
}

keyBuf := &bytes.Buffer{}
err = pem.Encode(keyBuf, &pem.Block{Type: "EC PRIVATE KEY", Bytes: keybytes})
if err != nil {
return nil, nil, fmt.Errorf("failed to encode private key: %v", err)
return nil, nil, fmt.Errorf("failed to encode private key: %w", err)
}

return certBuf.Bytes(), keyBuf.Bytes(), nil
Expand Down
12 changes: 6 additions & 6 deletions certgen/certgen_ed25519.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2015-2019 The Decred developers
// Copyright (c) 2015-2023 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -48,7 +48,7 @@ func NewEd25519TLSCertPair(organization string, validUntil time.Time, extraHosts
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return nil, nil, fmt.Errorf("failed to generate serial number: %s", err)
return nil, nil, fmt.Errorf("failed to generate serial number: %w", err)
}

host, err := os.Hostname()
Expand Down Expand Up @@ -123,24 +123,24 @@ func NewEd25519TLSCertPair(organization string, validUntil time.Time, extraHosts
derBytes, err := x509.CreateCertificate(rand.Reader, &template,
&template, priv.Public(), priv)
if err != nil {
return nil, nil, fmt.Errorf("failed to create certificate: %v", err)
return nil, nil, fmt.Errorf("failed to create certificate: %w", err)
}

certBuf := &bytes.Buffer{}
err = pem.Encode(certBuf, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
if err != nil {
return nil, nil, fmt.Errorf("failed to encode certificate: %v", err)
return nil, nil, fmt.Errorf("failed to encode certificate: %w", err)
}

keybytes, err := x509.MarshalPKCS8PrivateKey(priv)
if err != nil {
return nil, nil, fmt.Errorf("failed to marshal private key: %v", err)
return nil, nil, fmt.Errorf("failed to marshal private key: %w", err)
}

keyBuf := &bytes.Buffer{}
err = pem.Encode(keyBuf, &pem.Block{Type: "PRIVATE KEY", Bytes: keybytes})
if err != nil {
return nil, nil, fmt.Errorf("failed to encode private key: %v", err)
return nil, nil, fmt.Errorf("failed to encode private key: %w", err)
}

return certBuf.Bytes(), keyBuf.Bytes(), nil
Expand Down
12 changes: 6 additions & 6 deletions cmd/gencerts/gencerts.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2021 The Decred developers
// Copyright (c) 2020-2023 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -206,7 +206,7 @@ func randomX509SerialNumber() (*big.Int, error) {
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return nil, fmt.Errorf("failed to generate serial number: %s", err)
return nil, fmt.Errorf("failed to generate serial number: %w", err)
}
return serialNumber, nil
}
Expand Down Expand Up @@ -277,7 +277,7 @@ func generateAuthority(pub, priv interface{}, hosts []string, org string, years
buf := new(bytes.Buffer)
err = pem.Encode(buf, &pem.Block{Type: "CERTIFICATE", Bytes: cert})
if err != nil {
return nil, fmt.Errorf("failed to encode certificate: %v", err)
return nil, fmt.Errorf("failed to encode certificate: %w", err)
}
pemBlock := buf.Bytes()

Expand Down Expand Up @@ -317,7 +317,7 @@ func createIssuedCert(pub, caPriv interface{}, ca *x509.Certificate,
buf := new(bytes.Buffer)
err = pem.Encode(buf, &pem.Block{Type: "CERTIFICATE", Bytes: cert})
if err != nil {
return nil, fmt.Errorf("failed to encode certificate: %v", err)
return nil, fmt.Errorf("failed to encode certificate: %w", err)
}
pemBlock := buf.Bytes()

Expand All @@ -336,12 +336,12 @@ func createIssuedCert(pub, caPriv interface{}, ca *x509.Certificate,
func marshalPrivateKey(key interface{}) ([]byte, error) {
der, err := x509.MarshalPKCS8PrivateKey(key)
if err != nil {
return nil, fmt.Errorf("failed to marshal private key: %v", err)
return nil, fmt.Errorf("failed to marshal private key: %w", err)
}
buf := new(bytes.Buffer)
err = pem.Encode(buf, &pem.Block{Type: "PRIVATE KEY", Bytes: der})
if err != nil {
return nil, fmt.Errorf("failed to encode private key: %v", err)
return nil, fmt.Errorf("failed to encode private key: %w", err)
}
return buf.Bytes(), nil
}
4 changes: 2 additions & 2 deletions dcrutil/util.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2013, 2014 The btcsuite developers
// Copyright (c) 2015-2021 The Decred developers
// Copyright (c) 2015-2023 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -62,7 +62,7 @@ func VerifyMessage(address string, signature string, message string, params Addr
// Decode base64 signature.
sig, err := base64.StdEncoding.DecodeString(signature)
if err != nil {
return fmt.Errorf("malformed base64 encoding: %v", err)
return fmt.Errorf("malformed base64 encoding: %w", err)
}

// Validate the signature - this just shows that it was valid for any pubkey
Expand Down
8 changes: 4 additions & 4 deletions internal/blockchain/utxobackend_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2022 The Decred developers
// Copyright (c) 2021-2023 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -73,22 +73,22 @@ func TestConvertLdbErr(t *testing.T) {
gotErr := convertLdbErr(test.ldbErr, test.desc)

// Validate the error kind.
if gotErr.Err != test.want {
if !errors.Is(gotErr.Err, test.want) {
t.Errorf("%q: mismatched error kind:\nwant: %v\n got: %v\n",
test.name, test.want, gotErr.Err)
continue
}

wantDesc := fmt.Sprintf("%s: %v", test.desc, test.ldbErr)
// Validate the error description.
wantDesc := fmt.Sprintf("%s: %v", test.desc, test.ldbErr)
if gotErr.Description != wantDesc {
t.Errorf("%q: mismatched error description:\nwant: %v\n got: %v\n",
test.name, wantDesc, gotErr.Description)
continue
}

// Validate the raw error.
if gotErr.RawErr != test.ldbErr {
if !errors.Is(gotErr.RawErr, test.ldbErr) {
t.Errorf("%q: mismatched raw error:\nwant: %v\n got: %v\n",
test.name, test.ldbErr, gotErr.RawErr)
continue
Expand Down
25 changes: 12 additions & 13 deletions internal/fees/estimator.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018-2022 The Decred developers
// Copyright (c) 2018-2023 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -228,13 +228,13 @@ func NewEstimator(cfg *EstimatorConfig) (*Estimator, error) {
if cfg.DatabaseFile != "" {
db, err := leveldb.OpenFile(cfg.DatabaseFile, nil)
if err != nil {
return nil, fmt.Errorf("error opening estimator database: %v", err)
return nil, fmt.Errorf("error opening estimator database: %w", err)
}
res.db = db

err = res.loadFromDatabase(cfg.ReplaceBucketsOnLoad)
if err != nil {
return nil, fmt.Errorf("error loading estimator data from db: %v",
return nil, fmt.Errorf("error loading estimator data from db: %w",
err)
}
}
Expand Down Expand Up @@ -307,7 +307,7 @@ func (stats *Estimator) loadFromDatabase(replaceBuckets bool) error {

version, err := stats.db.Get(dbKeyVersion, nil)
if err != nil && !errors.Is(err, leveldb.ErrNotFound) {
return fmt.Errorf("error reading version from db: %v", err)
return fmt.Errorf("error reading version from db: %w", err)
}
if len(version) < 1 {
// No data in the file. Fill with the current config.
Expand All @@ -326,19 +326,19 @@ func (stats *Estimator) loadFromDatabase(replaceBuckets bool) error {

err := binary.Write(b, dbByteOrder, stats.bucketFeeBounds)
if err != nil {
return fmt.Errorf("error writing bucket fees to db: %v", err)
return fmt.Errorf("error writing bucket fees to db: %w", err)
}
batch.Put(dbKeyBucketFees, b.Bytes())

err = stats.db.Write(batch, nil)
if err != nil {
return fmt.Errorf("error writing initial estimator db file: %v",
return fmt.Errorf("error writing initial estimator db file: %w",
err)
}

err = stats.updateDatabase()
if err != nil {
return fmt.Errorf("error adding initial estimator data to db: %v",
return fmt.Errorf("error adding initial estimator data to db: %w",
err)
}

Expand All @@ -354,7 +354,7 @@ func (stats *Estimator) loadFromDatabase(replaceBuckets bool) error {
maxConfirmsBytes, err := stats.db.Get(dbKeyMaxConfirms, nil)
if err != nil {
return fmt.Errorf("error reading max confirmation range from db file: "+
"%v", err)
"%w", err)
}
if len(maxConfirmsBytes) != 4 {
return errors.New("wrong number of bytes in stored maxConfirms")
Expand All @@ -367,7 +367,7 @@ func (stats *Estimator) loadFromDatabase(replaceBuckets bool) error {

feesBytes, err := stats.db.Get(dbKeyBucketFees, nil)
if err != nil {
return fmt.Errorf("error reading fee bounds from db file: %v", err)
return fmt.Errorf("error reading fee bounds from db file: %w", err)
}
if feesBytes == nil {
return errors.New("fee bounds not found in database file")
Expand All @@ -381,7 +381,7 @@ func (stats *Estimator) loadFromDatabase(replaceBuckets bool) error {
err = binary.Read(bytes.NewReader(feesBytes), dbByteOrder,
&fileBucketFees)
if err != nil {
return fmt.Errorf("error decoding file bucket fees: %v", err)
return fmt.Errorf("error decoding file bucket fees: %w", err)
}

if !replaceBuckets {
Expand Down Expand Up @@ -450,7 +450,7 @@ func (stats *Estimator) loadFromDatabase(replaceBuckets bool) error {
}
err = iter.Error()
if err != nil {
return fmt.Errorf("error on bucket iterator: %v", err)
return fmt.Errorf("error on bucket iterator: %w", err)
}

stats.bucketFeeBounds = fileBucketFees
Expand Down Expand Up @@ -502,8 +502,7 @@ func (stats *Estimator) updateDatabase() error {

err := stats.db.Write(batch, nil)
if err != nil {
return fmt.Errorf("error writing update to estimator db file: %v",
err)
return fmt.Errorf("error writing update to estimator db file: %w", err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions internal/staging/banmanager/banmanager.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 The Decred developers
// Copyright (c) 2021-2023 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -119,7 +119,7 @@ func (bm *BanManager) AddPeer(p *peer.Peer) error {
host, _, err := net.SplitHostPort(p.Addr())
if err != nil {
p.Disconnect()
return fmt.Errorf("cannot split hostport %v", err)
return fmt.Errorf("cannot split hostport %w", err)
}

bm.mtx.Lock()
Expand Down
2 changes: 1 addition & 1 deletion rpcclient/infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ func (c *Client) handleSendPostMessage(details *sendPostDetails) {
respBytes, err := io.ReadAll(httpResponse.Body)
httpResponse.Body.Close()
if err != nil {
err = fmt.Errorf("error reading json reply: %v", err)
err = fmt.Errorf("error reading json reply: %w", err)
jReq.responseChan <- &response{err: err}
return
}
Expand Down
Loading

0 comments on commit 79be34a

Please sign in to comment.