Skip to content

Commit 9ac020b

Browse files
updated error handling
1 parent 489b198 commit 9ac020b

File tree

18 files changed

+50
-51
lines changed

18 files changed

+50
-51
lines changed

api/admin/performance.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ func (p *Performance) StartCPUProfiler() error {
3939
return err
4040
}
4141
if err := pprof.StartCPUProfile(file); err != nil {
42-
// Close should return an error if it has already been called
43-
// Return the original error in case of error
44-
file.Close() // #nosec G104
42+
_ = file.Close() // Return the original error
4543
return err
4644
}
4745
runtime.SetMutexProfileFraction(1)
@@ -70,9 +68,7 @@ func (p *Performance) MemoryProfile() error {
7068
}
7169
runtime.GC() // get up-to-date statistics
7270
if err := pprof.WriteHeapProfile(file); err != nil {
73-
// Close should return an error if it has already been called
74-
// Return the original error in case of error
75-
file.Close() // #nosec G104
71+
_ = file.Close() // Return the original error
7672
return err
7773
}
7874
return file.Close()
@@ -87,9 +83,7 @@ func (p *Performance) LockProfile() error {
8783

8884
profile := pprof.Lookup("mutex")
8985
if err := profile.WriteTo(file, 1); err != nil {
90-
// Close should return an error if it has already been called
91-
// Return the original error in case of error
92-
file.Close() // #nosec G104
86+
_ = file.Close() // Return the original error
9387
return err
9488
}
9589
return file.Close()

api/ipcs/server.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@ func (ipc *IPCs) PublishBlockchain(r *http.Request, args *PublishBlockchainArgs,
8989

9090
if err = sock.Listen(url); err != nil {
9191
ipc.log.Error("can't listen on pub socket: %s", err)
92-
// Closing the socket should only error if it has already been closed
93-
// #nosec G104
94-
sock.Close()
92+
_ = sock.Close() // Return the original error
9593
return err
9694
}
9795

@@ -101,9 +99,7 @@ func (ipc *IPCs) PublishBlockchain(r *http.Request, args *PublishBlockchainArgs,
10199
}
102100
if err := ipc.events.RegisterChain(chainID, "ipc", chainIPC); err != nil {
103101
ipc.log.Error("couldn't register event: %s", err)
104-
// Closing the socket should only error if it has already been closed
105-
// #nosec G104
106-
sock.Close()
102+
_ = sock.Close() // Return the original error
107103
return err
108104
}
109105

chains/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ func (m *manager) buildChain(chainParams ChainParameters) (*chain, error) {
379379
ctx.Lock.Lock()
380380
defer ctx.Lock.Unlock()
381381
if err := chain.Engine.Startup(); err != nil {
382-
chain.Ctx.Log.Error("failed to start consensus engine: %w", err)
382+
chain.Ctx.Log.Error("failed to start consensus engine: %s", err)
383383
chain.Handler.Shutdown()
384384
}
385385
})

database/memdb/db.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,15 @@ func (db *Database) NewIteratorWithStartAndPrefix(start, prefix []byte) database
142142
func (db *Database) Stat(property string) (string, error) { return "", database.ErrNotFound }
143143

144144
// Compact implements the Database interface
145-
func (db *Database) Compact(start []byte, limit []byte) error { return nil }
145+
func (db *Database) Compact(start []byte, limit []byte) error {
146+
db.lock.RLock()
147+
defer db.lock.RUnlock()
148+
149+
if db.db == nil {
150+
return database.ErrClosed
151+
}
152+
return nil
153+
}
146154

147155
type keyValue struct {
148156
key []byte

database/rpcdb/db_client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ func (it *iterator) Next() bool {
218218
resp, err := it.db.client.IteratorNext(context.Background(), &rpcdbproto.IteratorNextRequest{
219219
Id: it.id,
220220
})
221+
it.errs.Add(updateError(err))
221222

222223
if err != nil {
223-
it.errs.Add(err)
224224
return false
225225
}
226226
it.key = resp.Key
@@ -252,7 +252,7 @@ func (it *iterator) Release() {
252252
_, err := it.db.client.IteratorRelease(context.Background(), &rpcdbproto.IteratorReleaseRequest{
253253
Id: it.id,
254254
})
255-
it.errs.Add(err)
255+
it.errs.Add(updateError(err))
256256
}
257257

258258
// updateError sets the error value to the errors required by the Database

database/test_database.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -731,13 +731,17 @@ func TestStatNoPanic(t *testing.T, db Database) {
731731
t.Fatalf("Unexpected error on batch.Put: %s", err)
732732
}
733733

734-
db.Stat("") // #nosec G104
734+
// Stat could error or not redpending on the implementation, but it
735+
// shouldn't panic
736+
_, _ = db.Stat("")
735737

736738
if err := db.Close(); err != nil {
737739
t.Fatalf("Unexpected error on db.Close: %s", err)
738740
}
739741

740-
db.Stat("") // #nosec G104
742+
// Stat could error or not redpending on the implementation, but it
743+
// shouldn't panic
744+
_, _ = db.Stat("")
741745
}
742746

743747
// TestCompactNoPanic ...
@@ -759,11 +763,15 @@ func TestCompactNoPanic(t *testing.T, db Database) {
759763
t.Fatalf("Unexpected error on batch.Put: %s", err)
760764
}
761765

762-
db.Compact(nil, nil) // #nosec G104
766+
if err := db.Compact(nil, nil); err != nil {
767+
t.Fatalf("Unexpected error on db.Compact")
768+
}
763769

764770
if err := db.Close(); err != nil {
765771
t.Fatalf("Unexpected error on db.Close: %s", err)
766772
}
767773

768-
db.Compact(nil, nil) // #nosec G104
774+
if err := db.Compact(nil, nil); err != ErrClosed {
775+
t.Fatalf("Expected error %s on db.Close but got %s", ErrClosed, err)
776+
}
769777
}

nat/nat.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (dev *Mapper) keepPortMapping(mappedPort chan<- uint16, protocol string,
103103

104104
dev.log.Debug("Unmap protocol %s external port %d", protocol, extPort)
105105
if err := dev.r.UnmapPort(protocol, intPort, extPort); err != nil {
106-
dev.log.Error("Error unmapping port %d to %d: %w", intPort, extPort, err)
106+
dev.log.Debug("Error unmapping port %d to %d: %s", intPort, extPort, err)
107107
}
108108

109109
dev.wg.Done()

network/network.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,6 @@ func NewNetwork(
228228
pingPongTimeout time.Duration,
229229
pingFrequency time.Duration,
230230
) Network {
231-
// Ignore weak randomness warning because strong randomness is unnecessary for
232-
// creating the nodeID
233-
// #nosec G404
234231
net := &network{
235232
log: log,
236233
id: id,
@@ -269,7 +266,7 @@ func NewNetwork(
269266
peers: make(map[[20]byte]*peer),
270267
}
271268
if err := net.initialize(registerer); err != nil {
272-
log.Error("error initializing network metrics: %w", err)
269+
log.Warn("initializing network metrics failed with: %s", err)
273270
}
274271
net.executor.Initialize()
275272
net.heartbeat()
@@ -404,7 +401,7 @@ func (n *network) Accepted(validatorID ids.ShortID, chainID ids.ID, requestID ui
404401
func (n *network) GetAncestors(validatorID ids.ShortID, chainID ids.ID, requestID uint32, containerID ids.ID) {
405402
msg, err := n.b.GetAncestors(chainID, requestID, containerID)
406403
if err != nil {
407-
n.log.Error("failed to build GetAncestors message: %w", err)
404+
n.log.Error("failed to build GetAncestors message: %s", err)
408405
return
409406
}
410407

@@ -707,7 +704,7 @@ func (n *network) Close() error {
707704
n.closed = true
708705
err := n.listener.Close()
709706
if err != nil {
710-
n.log.Error("Error while closing network listener: %w", err)
707+
n.log.Debug("closing network listener failed with: %s", err)
711708
}
712709

713710
peersToClose := []*peer(nil)

snow/engine/snowman/bootstrapper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func (b *bootstrapper) MultiPut(vdr ids.ShortID, requestID uint32, blks [][]byte
153153

154154
wantedBlk, err := b.VM.ParseBlock(blks[0]) // the block we requested
155155
if err != nil {
156-
b.BootstrapConfig.Context.Log.Debug("Failed to parse requested block %s: %w", wantedBlkID, err)
156+
b.BootstrapConfig.Context.Log.Debug("Failed to parse requested block %s: %s", wantedBlkID, err)
157157
return b.fetch(wantedBlkID)
158158
} else if actualID := wantedBlk.ID(); !actualID.Equals(wantedBlkID) {
159159
b.BootstrapConfig.Context.Log.Debug("expected the first block to be the requested block, %s, but is %s", wantedBlk, actualID)
@@ -162,7 +162,7 @@ func (b *bootstrapper) MultiPut(vdr ids.ShortID, requestID uint32, blks [][]byte
162162

163163
for _, blkBytes := range blks {
164164
if _, err := b.VM.ParseBlock(blkBytes); err != nil { // persists the block
165-
b.BootstrapConfig.Context.Log.Debug("Failed to parse block: %w", err)
165+
b.BootstrapConfig.Context.Log.Debug("Failed to parse block: %s", err)
166166
b.BootstrapConfig.Context.Log.Verbo("block: %s", formatting.DumpBytes{Bytes: blkBytes})
167167
}
168168
}

snow/networking/router/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (h *Handler) Initialize(
125125
) {
126126
h.ctx = engine.Context()
127127
if err := h.metrics.Initialize(namespace, metrics); err != nil {
128-
h.ctx.Log.Error("Error initializing handler metrics: %w", err)
128+
h.ctx.Log.Warn("initializing handler metrics errored with: %s", err)
129129
}
130130
h.reliableMsgsSema = make(chan struct{}, 1)
131131
h.closed = make(chan struct{})

0 commit comments

Comments
 (0)