Skip to content

Commit 3a8c359

Browse files
committed
p2p: wrap internal error in new error type
Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
1 parent f5e0ead commit 3a8c359

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

p2p/metrics.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func markDialError(err error) {
6868
return
6969
}
7070

71+
var e protoHandshakeError
7172
switch {
7273
case errors.Is(err, DiscTooManyPeers):
7374
dialTooManyPeers.Mark(1)
@@ -81,7 +82,7 @@ func markDialError(err error) {
8182
dialUnexpectedIdentity.Mark(1)
8283
case errors.Is(err, errEncHandshakeError):
8384
dialEncHandshakeError.Mark(1)
84-
case errors.Is(err, errProtoHandshakeError):
85+
case errors.As(err, &e):
8586
dialProtoHandshakeError.Mark(1)
8687
}
8788
}

p2p/server.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,15 @@ const (
6666
)
6767

6868
var (
69-
errServerStopped = errors.New("server stopped")
70-
errEncHandshakeError = errors.New("rlpx enc error")
71-
errProtoHandshakeError = errors.New("rlpx proto error")
69+
errServerStopped = errors.New("server stopped")
70+
errEncHandshakeError = errors.New("rlpx enc error")
7271
)
7372

73+
type protoHandshakeError struct{ err error }
74+
75+
func (e *protoHandshakeError) Error() string { return fmt.Sprintf("rlpx proto error: %s", e.err) }
76+
func (e *protoHandshakeError) Unwrap() error { return e.err }
77+
7478
// Server manages all peer connections.
7579
type Server struct {
7680
// Config fields may not be modified while the server is running.
@@ -908,7 +912,7 @@ func (srv *Server) setupConn(c *conn, dialDest *enode.Node) error {
908912
if err != nil {
909913
clog.Trace("Failed p2p handshake", "err", err)
910914
//Wrapping both errors for later inspection
911-
return fmt.Errorf("%w: %w", errProtoHandshakeError, err)
915+
return &protoHandshakeError{err: err}
912916
}
913917
if id := c.node.ID(); !bytes.Equal(crypto.Keccak256(phs.ID), id[:]) {
914918
clog.Trace("Wrong devp2p handshake identity", "phsid", hex.EncodeToString(phs.ID))

0 commit comments

Comments
 (0)