From e5334ed29620e108da3b10bcd9c820004a13f3ea Mon Sep 17 00:00:00 2001 From: Hlib Kanunnikov Date: Tue, 27 Jun 2023 20:21:32 +0200 Subject: [PATCH] basichost / blankhost: wrap errors (#2331) --- p2p/host/basic/basic_host.go | 12 ++++++------ p2p/host/basic/basic_host_test.go | 2 +- p2p/host/blank/blank.go | 7 +++++-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/p2p/host/basic/basic_host.go b/p2p/host/basic/basic_host.go index 37cfa10995..7a0e37dff4 100644 --- a/p2p/host/basic/basic_host.go +++ b/p2p/host/basic/basic_host.go @@ -647,7 +647,7 @@ func (h *BasicHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.I if errors.Is(err, network.ErrNoConn) { return nil, errors.New("connection failed") } - return nil, err + return nil, fmt.Errorf("failed to open stream: %w", err) } // Wait for any in-progress identifies on the connection to finish. This @@ -659,7 +659,7 @@ func (h *BasicHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.I case <-h.ids.IdentifyWait(s.Conn()): case <-ctx.Done(): _ = s.Reset() - return nil, ctx.Err() + return nil, fmt.Errorf("identify failed to complete: %w", ctx.Err()) } pref, err := h.preferredProtocol(p, pids) @@ -688,13 +688,13 @@ func (h *BasicHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.I case err = <-errCh: if err != nil { s.Reset() - return nil, err + return nil, fmt.Errorf("failed to negotiate protocol: %w", err) } case <-ctx.Done(): s.Reset() // wait for `SelectOneOf` to error out because of resetting the stream. <-errCh - return nil, ctx.Err() + return nil, fmt.Errorf("failed to negotiate protocol: %w", ctx.Err()) } s.SetProtocol(selected) @@ -740,7 +740,7 @@ func (h *BasicHost) dialPeer(ctx context.Context, p peer.ID) error { log.Debugf("host %s dialing %s", h.ID(), p) c, err := h.Network().DialPeer(ctx, p) if err != nil { - return err + return fmt.Errorf("failed to dial: %w", err) } // TODO: Consider removing this? On one hand, it's nice because we can @@ -751,7 +751,7 @@ func (h *BasicHost) dialPeer(ctx context.Context, p peer.ID) error { select { case <-h.ids.IdentifyWait(c): case <-ctx.Done(): - return ctx.Err() + return fmt.Errorf("identify failed to complete: %w", ctx.Err()) } log.Debugf("host %s finished dialing %s", h.ID(), p) diff --git a/p2p/host/basic/basic_host_test.go b/p2p/host/basic/basic_host_test.go index 5c1babd9d5..1093da3121 100644 --- a/p2p/host/basic/basic_host_test.go +++ b/p2p/host/basic/basic_host_test.go @@ -739,7 +739,7 @@ func TestNegotiationCancel(t *testing.T) { select { case err := <-errCh: - require.Equal(t, err, context.Canceled) + require.ErrorIs(t, err, context.Canceled) case <-time.After(500 * time.Millisecond): // failed to cancel t.Fatal("expected negotiation to be canceled") diff --git a/p2p/host/blank/blank.go b/p2p/host/blank/blank.go index 9f3daeff23..24304498b0 100644 --- a/p2p/host/blank/blank.go +++ b/p2p/host/blank/blank.go @@ -136,6 +136,9 @@ func (bh *BlankHost) Connect(ctx context.Context, ai peer.AddrInfo) error { } _, err := bh.Network().DialPeer(ctx, ai.ID) + if err != nil { + return fmt.Errorf("failed to dial: %w", err) + } return err } @@ -150,13 +153,13 @@ func (bh *BlankHost) ID() peer.ID { func (bh *BlankHost) NewStream(ctx context.Context, p peer.ID, protos ...protocol.ID) (network.Stream, error) { s, err := bh.n.NewStream(ctx, p) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to open stream: %w", err) } selected, err := mstream.SelectOneOf(protos, s) if err != nil { s.Reset() - return nil, err + return nil, fmt.Errorf("failed to negotiate protocol: %w", err) } s.SetProtocol(selected)