Skip to content
Open
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
2 changes: 1 addition & 1 deletion clientconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
case err == nil || !cc.dopts.returnLastError:
conn, err = nil, ctx.Err()
default:
conn, err = nil, fmt.Errorf("%v: %v", ctx.Err(), err)
conn, err = nil, fmt.Errorf("%v: %w", err, ctx.Err())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we are returning wrapped errors, the inner error has become part of our public API. We must avoid breaking users who rely on errors.Is in the future. I think we should ensure all possible wrapped errors are explicitly documented in the godoc.

See go/go-style/best-practices#documentation-conventions-errors

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why reverse the order of the text output?

Also can we switch this outer select to:

ctxErr := ctx.Err()
if ctxErr == nil {
  return // use err as-is
}
switch { ..... }

But then that raises a question for me: if we aren't doing returnLastError, but FailOnNonTempDialError is set, what does that even mean? In this case, it looks like we are returning the connection error, unless there is also a context error, in which case we return it instead?

Should FailOnNonTempDialError imply both WithBlock and WithReturnConnectionError? I guess it shouldn't imply WithBlock but it seems like it should set returnLastError otherwise it's unpredictable.

}
default:
}
Expand Down
7 changes: 6 additions & 1 deletion dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package grpc

import (
"context"
"errors"
"fmt"
"net"
"strings"
Expand Down Expand Up @@ -162,7 +163,11 @@ func (s) TestDialWaitsForServerSettingsAndFails(t *testing.T) {
client.Close()
t.Fatalf("Unexpected success (err=nil) while dialing")
}
expectedMsg := "server preface"
// The error should wrap context.DeadlineExceeded.
if !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("DialContext() returned %v, want %v", err, context.DeadlineExceeded)
}
const expectedMsg = "error reading server preface"
if !strings.Contains(err.Error(), context.DeadlineExceeded.Error()) || !strings.Contains(err.Error(), expectedMsg) {
t.Fatalf("DialContext(_) = %v; want a message that includes both %q and %q", err, context.DeadlineExceeded.Error(), expectedMsg)
}
Expand Down
Loading