]> Cypherpunks repositories - gostls13.git/commitdiff
net: make ErrClosed and ParseError implement net.Error
authorIan Lance Taylor <iant@golang.org>
Fri, 2 Apr 2021 21:36:36 +0000 (14:36 -0700)
committerIan Lance Taylor <iant@golang.org>
Mon, 5 Apr 2021 19:15:53 +0000 (19:15 +0000)
Fixes #45357

Change-Id: Iafd41fff232a89be4c88d4b1d66bc3c04d888bcc
Reviewed-on: https://go-review.googlesource.com/c/go/+/307030
Trust: Ian Lance Taylor <iant@golang.org>
Trust: Josh Bleecher Snyder <josharian@gmail.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
src/internal/poll/fd.go
src/net/net.go
src/net/net_test.go

index b72ea3d55c2450ac216a7e599e1dc59ee741aec9..69a90054d3575f988499b44ec9e1311bd643ffd1 100644 (file)
@@ -13,11 +13,22 @@ import (
        "errors"
 )
 
-// ErrNetClosing is returned when a network descriptor is used after
-// it has been closed. Keep this string consistent because of issue
-// #4373: since historically programs have not been able to detect
+// errNetClosing is the type of the variable ErrNetClosing.
+// This is used to implement the net.Error interface.
+type errNetClosing struct{}
+
+// Error returns the error message for ErrNetClosing.
+// Keep this string consistent because of issue #4373:
+// since historically programs have not been able to detect
 // this error, they look for the string.
-var ErrNetClosing = errors.New("use of closed network connection")
+func (e errNetClosing) Error() string { return "use of closed network connection" }
+
+func (e errNetClosing) Timeout() bool   { return false }
+func (e errNetClosing) Temporary() bool { return false }
+
+// ErrNetClosing is returned when a network descriptor is used after
+// it has been closed.
+var ErrNetClosing = errNetClosing{}
 
 // ErrFileClosing is returned when a file descriptor is used after it
 // has been closed.
index 7e172b708e054d473d15074162610566601b4e0c..a7c65fff79526d1ce70fef7a96eaafd9ff06dd18 100644 (file)
@@ -539,6 +539,9 @@ type ParseError struct {
 
 func (e *ParseError) Error() string { return "invalid " + e.Type + ": " + e.Text }
 
+func (e *ParseError) Timeout() bool   { return false }
+func (e *ParseError) Temporary() bool { return false }
+
 type AddrError struct {
        Err  string
        Addr string
@@ -642,7 +645,7 @@ var errClosed = poll.ErrNetClosing
 // another goroutine before the I/O is completed. This may be wrapped
 // in another error, and should normally be tested using
 // errors.Is(err, net.ErrClosed).
-var ErrClosed = errClosed
+var ErrClosed error = errClosed
 
 type writerOnly struct {
        io.Writer
index 6d6299e74ad6e5570ed2a85fcab606179a8c99f6..6e7be4db23c1e2a26733bf251ff47546efcddd0d 100644 (file)
@@ -588,3 +588,23 @@ func TestNotTemporaryRead(t *testing.T) {
        }
        withTCPConnPair(t, client, server)
 }
+
+// The various errors should implement the Error interface.
+func TestErrors(t *testing.T) {
+       var (
+               _ Error = &OpError{}
+               _ Error = &ParseError{}
+               _ Error = &AddrError{}
+               _ Error = UnknownNetworkError("")
+               _ Error = InvalidAddrError("")
+               _ Error = &timeoutError{}
+               _ Error = &DNSConfigError{}
+               _ Error = &DNSError{}
+       )
+
+       // ErrClosed was introduced as type error, so we can't check
+       // it using a declaration.
+       if _, ok := ErrClosed.(Error); !ok {
+               t.Fatal("ErrClosed does not implement Error")
+       }
+}