Skip to content

Commit 04c4e2d

Browse files
committed
multi: Misc error simplification and cleanup.
This does some basic error-related housekeeping to simplify all print-related formatting as follows: - Use the error directly as opposed to unnecessarily manually invoking the Error method - Avoid unneeded formatting directives when the output only consists of the error itself - Use const strings when matching on fixed error strings - Wrap errors when using fmt.Errorf
1 parent 3339a78 commit 04c4e2d

File tree

19 files changed

+64
-75
lines changed

19 files changed

+64
-75
lines changed

addrmgr/addrmanager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2013-2014 The btcsuite developers
2-
// Copyright (c) 2015-2024 The Decred developers
2+
// Copyright (c) 2015-2025 The Decred developers
33
// Use of this source code is governed by an ISC
44
// license that can be found in the LICENSE file.
55

@@ -605,7 +605,7 @@ func TestGood(t *testing.T) {
605605

606606
expectedErrMsg := fmt.Sprintf("%s is not marked as a new address", addrA)
607607
if err.Error() != expectedErrMsg {
608-
t.Fatalf("unexpected error str: got %s, want %s", err.Error(), expectedErrMsg)
608+
t.Fatalf("unexpected error str: got %s, want %s", err, expectedErrMsg)
609609
}
610610
}
611611

blockchain/stake/internal/ticketdb/chainio_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2016-2022 The Decred developers
1+
// Copyright (c) 2016-2025 The Decred developers
22
// Use of this source code is governed by an ISC
33
// license that can be found in the LICENSE file.
44

@@ -398,7 +398,7 @@ func TestLiveDatabase(t *testing.T) {
398398
// Initialize the database, then try to read the version.
399399
err = testDb.Update(DbCreate)
400400
if err != nil {
401-
t.Fatalf("%v", err.Error())
401+
t.Fatal(err)
402402
}
403403

404404
var dbi *DatabaseInfo
@@ -407,7 +407,7 @@ func TestLiveDatabase(t *testing.T) {
407407
return err
408408
})
409409
if err != nil {
410-
t.Fatalf("%v", err.Error())
410+
t.Fatal(err)
411411
}
412412
if dbi.Version != currentDatabaseVersion {
413413
t.Fatalf("bad version after reading from DB; want %v, got %v",
@@ -442,7 +442,7 @@ func TestLiveDatabase(t *testing.T) {
442442
return nil
443443
})
444444
if err != nil {
445-
t.Fatalf("%v", err.Error())
445+
t.Fatal(err)
446446
}
447447

448448
var treap *tickettreap.Immutable
@@ -452,7 +452,7 @@ func TestLiveDatabase(t *testing.T) {
452452
return err
453453
})
454454
if err != nil {
455-
t.Fatalf("%v", err.Error())
455+
t.Fatal(err)
456456
}
457457
treap.ForEach(func(k tickettreap.Key, v *tickettreap.Value) bool {
458458
ticketMap2[k] = v
@@ -461,6 +461,6 @@ func TestLiveDatabase(t *testing.T) {
461461
})
462462

463463
if !reflect.DeepEqual(ticketMap, ticketMap2) {
464-
t.Fatalf("not same ticket maps")
464+
t.Fatal("not same ticket maps")
465465
}
466466
}

blockchain/stake/staketx_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2015-2024 The Decred developers
1+
// Copyright (c) 2015-2025 The Decred developers
22
// Use of this source code is governed by an ISC
33
// license that can be found in the LICENSE file.
44

@@ -1163,8 +1163,7 @@ func TestGetSStxNullOutputAmounts(t *testing.T) {
11631163
amtTicket)
11641164

11651165
// len commit to amts != len change amts
1166-
lenErrStr := "amounts was not equal in length " +
1167-
"to change amounts!"
1166+
const lenErrStr = "amounts was not equal in length to change amounts!"
11681167
if err == nil || err.Error() != lenErrStr {
11691168
t.Errorf("TestGetSStxNullOutputAmounts unexpected error: %v", err)
11701169
}
@@ -1174,7 +1173,7 @@ func TestGetSStxNullOutputAmounts(t *testing.T) {
11741173
commitAmts,
11751174
changeAmts,
11761175
int64(0x00000000))
1177-
tooSmallErrStr := "committed amount was too small!"
1176+
const tooSmallErrStr = "committed amount was too small!"
11781177
if err == nil || err.Error() != tooSmallErrStr {
11791178
t.Errorf("TestGetSStxNullOutputAmounts unexpected error: %v", err)
11801179
}

cmd/addblock/import.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2013-2016 The btcsuite developers
2-
// Copyright (c) 2015-2022 The Decred developers
2+
// Copyright (c) 2015-2025 The Decred developers
33
// Use of this source code is governed by an ISC
44
// license that can be found in the LICENSE file.
55

@@ -159,8 +159,7 @@ out:
159159
// notify the status handler with the error and bail.
160160
serializedBlock, err := bi.readBlock()
161161
if err != nil {
162-
bi.errChan <- fmt.Errorf("error reading from input "+
163-
"file: %v", err.Error())
162+
bi.errChan <- fmt.Errorf("error reading from input file: %w", err)
164163
break out
165164
}
166165

database/cmd/dbtool/insecureimport.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2015-2016 The btcsuite developers
2-
// Copyright (c) 2016-2020 The Decred developers
2+
// Copyright (c) 2016-2025 The Decred developers
33
// Use of this source code is governed by an ISC
44
// license that can be found in the LICENSE file.
55

@@ -171,8 +171,7 @@ out:
171171
// notify the status handler with the error and bail.
172172
serializedBlock, err := bi.readBlock()
173173
if err != nil {
174-
bi.errChan <- fmt.Errorf("error reading from input "+
175-
"file: %v", err.Error())
174+
bi.errChan <- fmt.Errorf("error reading from input file: %w", err)
176175
break out
177176
}
178177

database/ffldb/interface_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2015-2016 The btcsuite developers
2-
// Copyright (c) 2016-2023 The Decred developers
2+
// Copyright (c) 2016-2025 The Decred developers
33
// Use of this source code is governed by an ISC
44
// license that can be found in the LICENSE file.
55

@@ -74,15 +74,15 @@ func loadBlocks(t *testing.T, dataFile string, network wire.CurrencyNet) ([]*dcr
7474

7575
// Decode the blockchain into the map.
7676
if err := bcDecoder.Decode(&blockChain); err != nil {
77-
t.Errorf("error decoding test blockchain: %v", err.Error())
77+
t.Errorf("error decoding test blockchain: %v", err)
7878
}
7979

8080
// Fetch blocks 1 to 168 and perform various tests.
8181
blocks := make([]*dcrutil.Block, 169)
8282
for i := 0; i <= 168; i++ {
8383
bl, err := dcrutil.NewBlockFromBytes(blockChain[int64(i)])
8484
if err != nil {
85-
t.Errorf("NewBlockFromBytes error: %v", err.Error())
85+
t.Errorf("NewBlockFromBytes error: %v", err)
8686
}
8787

8888
blocks[i] = bl

database/ffldb/whitebox_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2015-2016 The btcsuite developers
2-
// Copyright (c) 2016-2023 The Decred developers
2+
// Copyright (c) 2016-2025 The Decred developers
33
// Use of this source code is governed by an ISC
44
// license that can be found in the LICENSE file.
55

@@ -69,15 +69,15 @@ func loadBlocks(t *testing.T, dataFile string, network wire.CurrencyNet) ([]*dcr
6969

7070
// Decode the blockchain into the map.
7171
if err := bcDecoder.Decode(&blockChain); err != nil {
72-
t.Errorf("error decoding test blockchain: %v", err.Error())
72+
t.Errorf("error decoding test blockchain: %v", err)
7373
}
7474

7575
// Fetch blocks 1 to 168 and perform various tests.
7676
blocks := make([]*dcrutil.Block, 169)
7777
for i := 0; i <= 168; i++ {
7878
bl, err := dcrutil.NewBlockFromBytes(blockChain[int64(i)])
7979
if err != nil {
80-
t.Errorf("NewBlockFromBytes error: %v", err.Error())
80+
t.Errorf("NewBlockFromBytes error: %v", err)
8181
}
8282

8383
blocks[i] = bl

dcrd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func dcrdMain() error {
123123
if cfg.CPUProfile != "" {
124124
f, err := os.Create(cfg.CPUProfile)
125125
if err != nil {
126-
dcrdLog.Errorf("Unable to create cpu profile: %v", err.Error())
126+
dcrdLog.Errorf("Unable to create cpu profile: %v", err)
127127
return err
128128
}
129129
pprof.StartCPUProfile(f)

internal/blockchain/chain_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2013-2016 The btcsuite developers
2-
// Copyright (c) 2015-2023 The Decred developers
2+
// Copyright (c) 2015-2025 The Decred developers
33
// Use of this source code is governed by an ISC
44
// license that can be found in the LICENSE file.
55

@@ -331,19 +331,19 @@ func TestBlockchainFunctions(t *testing.T) {
331331

332332
// Decode the blockchain into the map.
333333
if err := bcDecoder.Decode(&blockChain); err != nil {
334-
t.Errorf("error decoding test blockchain: %v", err.Error())
334+
t.Errorf("error decoding test blockchain: %v", err)
335335
}
336336

337337
// Insert blocks 1 to 168 and perform various tests.
338338
for i := 1; i <= 168; i++ {
339339
bl, err := dcrutil.NewBlockFromBytes(blockChain[int64(i)])
340340
if err != nil {
341-
t.Errorf("NewBlockFromBytes error: %v", err.Error())
341+
t.Errorf("NewBlockFromBytes error: %v", err)
342342
}
343343

344344
_, err = chain.ProcessBlock(bl)
345345
if err != nil {
346-
t.Fatalf("ProcessBlock error at height %v: %v", i, err.Error())
346+
t.Fatalf("ProcessBlock error at height %v: %v", i, err)
347347
}
348348
}
349349

internal/blockchain/chainio_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2015-2016 The btcsuite developers
2-
// Copyright (c) 2015-2022 The Decred developers
2+
// Copyright (c) 2015-2025 The Decred developers
33
// Use of this source code is governed by an ISC
44
// license that can be found in the LICENSE file.
55

@@ -69,13 +69,13 @@ func isNotInMainChainErr(err error) bool {
6969
// TestErrNotInMainChain ensures the functions related to errNotInMainChain work
7070
// as expected.
7171
func TestErrNotInMainChain(t *testing.T) {
72-
errStr := "no block at height 1 exists"
72+
const errStr = "no block at height 1 exists"
7373
err := error(errNotInMainChain(errStr))
7474

7575
// Ensure the stringized output for the error is as expected.
7676
if err.Error() != errStr {
7777
t.Fatalf("errNotInMainChain returned unexpected error string - "+
78-
"got %q, want %q", err.Error(), errStr)
78+
"got %q, want %q", err, errStr)
7979
}
8080

8181
// Ensure error is detected as the correct type.

0 commit comments

Comments
 (0)