From a485b0092bf37636bb8e1b8c6ed01023f2323f56 Mon Sep 17 00:00:00 2001 From: Samuel Stauffer Date: Wed, 13 Dec 2023 13:21:51 -0800 Subject: [PATCH 1/2] pgconn: normalize starTLS connection error Normalize the error that is returned by startTLS in pgconn.connect. This makes it possible to determine if the error was a context error. --- pgconn/pgconn.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pgconn/pgconn.go b/pgconn/pgconn.go index 1ccdc4db9..0203cf194 100644 --- a/pgconn/pgconn.go +++ b/pgconn/pgconn.go @@ -289,7 +289,7 @@ func connect(ctx context.Context, config *Config, fallbackConfig *FallbackConfig pgConn.contextWatcher.Unwatch() // Always unwatch `netConn` after TLS. if err != nil { netConn.Close() - return nil, &connectError{config: config, msg: "tls error", err: err} + return nil, &connectError{config: config, msg: "tls error", err: normalizeTimeoutError(ctx, err)} } pgConn.conn = nbTLSConn From 816032fdb542a136be6260ddd58d897b31944b26 Mon Sep 17 00:00:00 2001 From: Samuel Stauffer Date: Wed, 13 Dec 2023 17:44:10 -0800 Subject: [PATCH 2/2] Unwrap errors in normalizeTimeoutError --- pgconn/errors.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pgconn/errors.go b/pgconn/errors.go index 3c54bbec0..619578298 100644 --- a/pgconn/errors.go +++ b/pgconn/errors.go @@ -107,14 +107,15 @@ func (e *parseConfigError) Unwrap() error { } func normalizeTimeoutError(ctx context.Context, err error) error { - if err, ok := err.(net.Error); ok && err.Timeout() { + var netErr net.Error + if errors.As(err, &netErr) && netErr.Timeout() { if ctx.Err() == context.Canceled { // Since the timeout was caused by a context cancellation, the actual error is context.Canceled not the timeout error. return context.Canceled } else if ctx.Err() == context.DeadlineExceeded { return &errTimeout{err: ctx.Err()} } else { - return &errTimeout{err: err} + return &errTimeout{err: netErr} } } return err