From 0f76d656caf5487922fed1021d19857c4d567734 Mon Sep 17 00:00:00 2001 From: Seth Pellegrino Date: Fri, 28 Feb 2020 13:44:59 -0800 Subject: [PATCH] client: surface connection errors to callers This commit allows blocking clients to receive a more informative error message than "context deadline exceeded", which is especially helpful in tracking down persistent client misconfiguration (such as an invalid TLS certificate, an invalid server that's refusing connections, etc.) --- clientconn.go | 12 +++++++++++- clientconn_test.go | 5 +++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/clientconn.go b/clientconn.go index 293d2f62fd1e..4cb658b95d25 100644 --- a/clientconn.go +++ b/clientconn.go @@ -216,7 +216,14 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn * defer func() { select { case <-ctx.Done(): - conn, err = nil, ctx.Err() + switch { + case ctx.Err() == err: + conn = nil + case err == nil: + conn, err = nil, ctx.Err() + default: + conn, err = nil, fmt.Errorf("%v: %v", ctx.Err(), err) + } default: } }() @@ -321,6 +328,9 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn * } if !cc.WaitForStateChange(ctx, s) { // ctx got timeout or canceled. + if err = cc.blockingpicker.connectionError(); err != nil { + return nil, err + } return nil, ctx.Err() } } diff --git a/clientconn_test.go b/clientconn_test.go index 23a9c67bce07..269ac1b299ef 100644 --- a/clientconn_test.go +++ b/clientconn_test.go @@ -229,8 +229,9 @@ func (s) TestDialWaitsForServerSettingsAndFails(t *testing.T) { client.Close() t.Fatalf("Unexpected success (err=nil) while dialing") } - if err != context.DeadlineExceeded { - t.Fatalf("DialContext(_) = %v; want context.DeadlineExceeded", err) + expectedMsg := "server handshake" + 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) } <-done if numConns < 2 {