Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basichost / blankhost: wrap errors #2331

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,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 @@ -653,7 +653,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 @@ -682,13 +682,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 @@ -734,7 +734,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 @@ -745,7 +745,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