Skip to content

Commit

Permalink
basichost / blankhost: wrap errors (#2331)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan authored Jun 27, 2023
1 parent f1236d8 commit e5334ed
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
12 changes: 6 additions & 6 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion p2p/host/basic/basic_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 5 additions & 2 deletions p2p/host/blank/blank.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
Expand Down

0 comments on commit e5334ed

Please sign in to comment.