Skip to content

Commit f257f3d

Browse files
gennadylaventmanyacovm
authored andcommitted
[FAB-5764] Errors handling - 1
All packages under gossip except gossip/gossip. New errors created using github.com/pkg/errors, stack trace added to returned errors using WithStack. In case of returned error was incorporated in new one, now it is wrapped using Wrap. %+v added to each logger call that output error. Change-Id: I4c7a3517da6c34bedffd68fe336e940e68ebcfe3 Signed-off-by: Gennady Laventman <gennady@il.ibm.com>
1 parent 01adda9 commit f257f3d

File tree

13 files changed

+110
-103
lines changed

13 files changed

+110
-103
lines changed

gossip/comm/comm_impl.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package comm
99
import (
1010
"bytes"
1111
"crypto/tls"
12-
"errors"
1312
"fmt"
1413
"net"
1514
"reflect"
@@ -23,6 +22,7 @@ import (
2322
"github.com/hyperledger/fabric/gossip/util"
2423
proto "github.com/hyperledger/fabric/protos/gossip"
2524
"github.com/op/go-logging"
25+
"github.com/pkg/errors"
2626
"github.com/spf13/viper"
2727
"golang.org/x/net/context"
2828
"google.golang.org/grpc"
@@ -106,7 +106,7 @@ func NewCommInstance(s *grpc.Server, cert *tls.Certificate, idStore identity.Map
106106
dialOpts = append(dialOpts, grpc.WithTimeout(util.GetDurationOrDefault("peer.gossip.dialTimeout", defDialTimeout)))
107107
commInst, err := NewCommInstanceWithServer(-1, idStore, peerIdentity, secureDialOpts, dialOpts...)
108108
if err != nil {
109-
return nil, err
109+
return nil, errors.WithStack(err)
110110
}
111111

112112
if cert != nil {
@@ -163,7 +163,7 @@ func (c *commImpl) createConnection(endpoint string, expectedPKIID common.PKIidT
163163
dialOpts = append(dialOpts, c.opts...)
164164
cc, err = grpc.Dial(endpoint, dialOpts...)
165165
if err != nil {
166-
return nil, err
166+
return nil, errors.WithStack(err)
167167
}
168168

169169
cl := proto.NewGossipClient(cc)
@@ -172,7 +172,7 @@ func (c *commImpl) createConnection(endpoint string, expectedPKIID common.PKIidT
172172
defer cancel()
173173
if _, err = cl.Ping(ctx, &proto.Empty{}); err != nil {
174174
cc.Close()
175-
return nil, err
175+
return nil, errors.WithStack(err)
176176
}
177177

178178
ctx, cf := context.WithCancel(context.Background())
@@ -204,10 +204,10 @@ func (c *commImpl) createConnection(endpoint string, expectedPKIID common.PKIidT
204204
conn.handler = h
205205
return conn, nil
206206
}
207-
c.logger.Warning("Authentication failed:", err)
207+
c.logger.Warningf("Authentication failed: %+v", err)
208208
}
209209
cc.Close()
210-
return nil, err
210+
return nil, errors.WithStack(err)
211211
}
212212

213213
func (c *commImpl) Send(msg *proto.SignedGossipMessage, peers ...*RemotePeer) {
@@ -235,13 +235,15 @@ func (c *commImpl) sendToEndpoint(peer *RemotePeer, msg *proto.SignedGossipMessa
235235
conn, err := c.connStore.getConnection(peer)
236236
if err == nil {
237237
disConnectOnErr := func(err error) {
238-
c.logger.Warning(peer, "isn't responsive:", err)
238+
err = errors.WithStack(err)
239+
c.logger.Warningf("%v isn't responsive: %+v", peer, err)
239240
c.disconnect(peer.PKIID)
240241
}
241242
conn.send(msg, disConnectOnErr)
242243
return
243244
}
244-
c.logger.Warning("Failed obtaining connection for", peer, "reason:", err)
245+
err = errors.WithStack(err)
246+
c.logger.Warningf("Failed obtaining connection for %v reason: %+v", peer, err)
245247
c.disconnect(peer.PKIID)
246248
}
247249

@@ -263,15 +265,15 @@ func (c *commImpl) Probe(remotePeer *RemotePeer) error {
263265

264266
cc, err := grpc.Dial(remotePeer.Endpoint, dialOpts...)
265267
if err != nil {
266-
c.logger.Debug("Returning", err)
268+
c.logger.Debugf("Returning %v", err)
267269
return err
268270
}
269271
defer cc.Close()
270272
cl := proto.NewGossipClient(cc)
271273
ctx, cancel := context.WithTimeout(context.Background(), defConnTimeout)
272274
defer cancel()
273275
_, err = cl.Ping(ctx, &proto.Empty{})
274-
c.logger.Debug("Returning", err)
276+
c.logger.Debugf("Returning %v", err)
275277
return err
276278
}
277279

@@ -300,11 +302,11 @@ func (c *commImpl) Handshake(remotePeer *RemotePeer) (api.PeerIdentityType, erro
300302
}
301303
connInfo, err := c.authenticateRemotePeer(stream)
302304
if err != nil {
303-
c.logger.Warning("Authentication failed:", err)
305+
c.logger.Warningf("Authentication failed: %v", err)
304306
return nil, err
305307
}
306308
if len(remotePeer.PKIID) > 0 && !bytes.Equal(connInfo.ID, remotePeer.PKIID) {
307-
return nil, errors.New("PKI-ID of remote peer doesn't match expected PKI-ID")
309+
return nil, fmt.Errorf("PKI-ID of remote peer doesn't match expected PKI-ID")
308310
}
309311
return connInfo.Identity, nil
310312
}
@@ -424,7 +426,7 @@ func (c *commImpl) authenticateRemotePeer(stream stream) (*proto.ConnectionInfo,
424426
// TLS enabled but not detected on other side
425427
if useTLS && len(remoteCertHash) == 0 {
426428
c.logger.Warningf("%s didn't send TLS certificate", remoteAddress)
427-
return nil, errors.New("No TLS certificate")
429+
return nil, fmt.Errorf("No TLS certificate")
428430
}
429431

430432
cMsg, err = c.createConnectionMsg(c.PKIID, c.selfCertHash, c.peerIdentity, signer)
@@ -442,18 +444,18 @@ func (c *commImpl) authenticateRemotePeer(stream stream) (*proto.ConnectionInfo,
442444
receivedMsg := m.GetConn()
443445
if receivedMsg == nil {
444446
c.logger.Warning("Expected connection message from", remoteAddress, "but got", receivedMsg)
445-
return nil, errors.New("Wrong type")
447+
return nil, fmt.Errorf("Wrong type")
446448
}
447449

448450
if receivedMsg.PkiId == nil {
449451
c.logger.Warning("%s didn't send a pkiID", remoteAddress)
450-
return nil, errors.New("No PKI-ID")
452+
return nil, fmt.Errorf("No PKI-ID")
451453
}
452454

453455
c.logger.Debug("Received", receivedMsg, "from", remoteAddress)
454456
err = c.idMapper.Put(receivedMsg.PkiId, receivedMsg.Identity)
455457
if err != nil {
456-
c.logger.Warning("Identity store rejected", remoteAddress, ":", err)
458+
c.logger.Warningf("Identity store rejected %s : %v", remoteAddress, err)
457459
return nil, err
458460
}
459461

@@ -468,15 +470,15 @@ func (c *commImpl) authenticateRemotePeer(stream stream) (*proto.ConnectionInfo,
468470
// If the remote peer sent its TLS certificate, make sure it actually matches the TLS cert
469471
// that the peer used.
470472
if !bytes.Equal(remoteCertHash, receivedMsg.TlsCertHash) {
471-
return nil, fmt.Errorf("Expected %v in remote hash of TLS cert, but got %v", remoteCertHash, receivedMsg.TlsCertHash)
473+
return nil, errors.Errorf("Expected %v in remote hash of TLS cert, but got %v", remoteCertHash, receivedMsg.TlsCertHash)
472474
}
473475
verifier := func(peerIdentity []byte, signature, message []byte) error {
474476
pkiID := c.idMapper.GetPKIidOfCert(api.PeerIdentityType(peerIdentity))
475477
return c.idMapper.Verify(pkiID, signature, message)
476478
}
477479
err = m.Verify(receivedMsg.Identity, verifier)
478480
if err != nil {
479-
c.logger.Error("Failed verifying signature from", remoteAddress, ":", err)
481+
c.logger.Error("Failed verifying signature from %s : %v", remoteAddress, err)
480482
return nil, err
481483
}
482484
connInfo.Auth = &proto.AuthInfo{
@@ -496,7 +498,7 @@ func (c *commImpl) GossipStream(stream proto.Gossip_GossipStreamServer) error {
496498
}
497499
connInfo, err := c.authenticateRemotePeer(stream)
498500
if err != nil {
499-
c.logger.Error("Authentication failed:", err)
501+
c.logger.Errorf("Authentication failed: %v", err)
500502
return err
501503
}
502504
c.logger.Debug("Servicing", extractRemoteAddress(stream))
@@ -564,16 +566,16 @@ func readWithTimeout(stream interface{}, timeout time.Duration, address string)
564566
incChan <- msg
565567
}
566568
} else {
567-
panic(fmt.Errorf("Stream isn't a GossipStreamServer or a GossipStreamClient, but %v. Aborting", reflect.TypeOf(stream)))
569+
panic(errors.Errorf("Stream isn't a GossipStreamServer or a GossipStreamClient, but %v. Aborting", reflect.TypeOf(stream)))
568570
}
569571
}()
570572
select {
571573
case <-time.NewTicker(timeout).C:
572-
return nil, fmt.Errorf("Timed out waiting for connection message from %s", address)
574+
return nil, errors.Errorf("Timed out waiting for connection message from %s", address)
573575
case m := <-incChan:
574576
return m, nil
575577
case err := <-errChan:
576-
return nil, err
578+
return nil, errors.WithStack(err)
577579
}
578580
}
579581

@@ -593,7 +595,7 @@ func (c *commImpl) createConnectionMsg(pkiID common.PKIidType, certHash []byte,
593595
GossipMessage: m,
594596
}
595597
_, err := sMsg.Sign(signer)
596-
return sMsg, err
598+
return sMsg, errors.WithStack(err)
597599
}
598600

599601
type stream interface {

gossip/comm/conn.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ SPDX-License-Identifier: Apache-2.0
77
package comm
88

99
import (
10-
"errors"
1110
"sync"
1211
"sync/atomic"
1312

1413
"github.com/hyperledger/fabric/gossip/common"
1514
"github.com/hyperledger/fabric/gossip/util"
1615
proto "github.com/hyperledger/fabric/protos/gossip"
1716
"github.com/op/go-logging"
17+
"github.com/pkg/errors"
1818
"golang.org/x/net/context"
1919
"google.golang.org/grpc"
2020
)
@@ -103,7 +103,7 @@ func (cs *connectionStore) getConnection(peer *RemotePeer) (*connection, error)
103103

104104
// no one connected to us AND we failed connecting!
105105
if err != nil {
106-
return nil, err
106+
return nil, errors.WithStack(err)
107107
}
108108

109109
// at this point in the code, we created a connection to a remote peer
@@ -285,7 +285,7 @@ func (conn *connection) serviceConnection() error {
285285
conn.stopChan <- stop
286286
return nil
287287
case err := <-errChan:
288-
return err
288+
return errors.WithStack(err)
289289
case msg := <-msgChan:
290290
conn.handler(msg)
291291
}
@@ -304,7 +304,7 @@ func (conn *connection) writeToStream() {
304304
case m := <-conn.outBuff:
305305
err := stream.Send(m.envelope)
306306
if err != nil {
307-
go m.onErr(err)
307+
go m.onErr(errors.WithStack(err))
308308
return
309309
}
310310
case stop := <-conn.stopChan:
@@ -333,13 +333,13 @@ func (conn *connection) readFromStream(errChan chan error, msgChan chan *proto.S
333333
}
334334
if err != nil {
335335
errChan <- err
336-
conn.logger.Debug(conn.pkiID, "Got error, aborting:", err)
336+
conn.logger.Debugf("%v Got error, aborting: %+v", conn.pkiID, errors.WithStack(err))
337337
return
338338
}
339339
msg, err := envelope.ToGossipMessage()
340340
if err != nil {
341341
errChan <- err
342-
conn.logger.Warning(conn.pkiID, "Got error, aborting:", err)
342+
conn.logger.Warning("%v Got error, aborting: %+v", conn.pkiID, errors.WithStack(err))
343343
}
344344
msgChan <- msg
345345
}
@@ -350,8 +350,8 @@ func (conn *connection) getStream() stream {
350350
defer conn.Unlock()
351351

352352
if conn.clientStream != nil && conn.serverStream != nil {
353-
e := "Both client and server stream are not nil, something went wrong"
354-
conn.logger.Error(e)
353+
e := errors.New("Both client and server stream are not nil, something went wrong")
354+
conn.logger.Errorf("%+v", e)
355355
}
356356

357357
if conn.clientStream != nil {

gossip/comm/crypto.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ import (
1313
"crypto/tls"
1414
"crypto/x509"
1515
"encoding/pem"
16-
"errors"
1716
"fmt"
1817
"math/big"
1918
"os"
2019

20+
"errors"
21+
2122
"github.com/hyperledger/fabric/common/util"
2223
gutil "github.com/hyperledger/fabric/gossip/util"
2324
"golang.org/x/net/context"

gossip/comm/msg.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"sync"
1111

1212
proto "github.com/hyperledger/fabric/protos/gossip"
13+
"github.com/pkg/errors"
1314
)
1415

1516
// ReceivedMessageImpl is an implementation of ReceivedMessage
@@ -30,7 +31,8 @@ func (m *ReceivedMessageImpl) GetSourceEnvelope() *proto.Envelope {
3031
func (m *ReceivedMessageImpl) Respond(msg *proto.GossipMessage) {
3132
sMsg, err := msg.NoopSign()
3233
if err != nil {
33-
m.conn.logger.Error("Failed creating SignedGossipMessage:", err)
34+
err = errors.WithStack(err)
35+
m.conn.logger.Errorf("Failed creating SignedGossipMessage: %+v", err)
3436
return
3537
}
3638
m.conn.send(sMsg, func(e error) {})

gossip/common/metastate.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ package common
99
import (
1010
"bytes"
1111
"encoding/binary"
12+
13+
"github.com/pkg/errors"
1214
)
1315

1416
// NodeMetastate information to store the information about current
@@ -32,7 +34,7 @@ func (n *NodeMetastate) Bytes() ([]byte, error) {
3234
// with FromBytes function
3335
err := binary.Write(buffer, binary.BigEndian, *n)
3436
if err != nil {
35-
return nil, err
37+
return nil, errors.WithStack(err)
3638
}
3739
return buffer.Bytes(), nil
3840
}
@@ -56,7 +58,7 @@ func FromBytes(buf []byte) (*NodeMetastate, error) {
5658
// done using same order
5759
err := binary.Read(reader, binary.BigEndian, &state)
5860
if err != nil {
59-
return nil, err
61+
return nil, errors.WithStack(err)
6062
}
6163
return &state, nil
6264
}

0 commit comments

Comments
 (0)