[go1.15] Use errors.As to unwrap net errors

This commit is contained in:
Jordan Liggitt 2020-08-07 15:22:54 -04:00
parent f5334fad48
commit c61c60eb1f
3 changed files with 18 additions and 41 deletions

View File

@ -62,8 +62,11 @@ func JoinPreservingTrailingSlash(elem ...string) string {
// IsTimeout returns true if the given error is a network timeout error // IsTimeout returns true if the given error is a network timeout error
func IsTimeout(err error) bool { func IsTimeout(err error) bool {
neterr, ok := err.(net.Error) var neterr net.Error
return ok && neterr != nil && neterr.Timeout() if errors.As(err, &neterr) {
return neterr != nil && neterr.Timeout()
}
return false
} }
// IsProbableEOF returns true if the given error resembles a connection termination // IsProbableEOF returns true if the given error resembles a connection termination
@ -76,7 +79,8 @@ func IsProbableEOF(err error) bool {
if err == nil { if err == nil {
return false return false
} }
if uerr, ok := err.(*url.Error); ok { var uerr *url.Error
if errors.As(err, &uerr) {
err = uerr.Err err = uerr.Err
} }
msg := err.Error() msg := err.Error()

View File

@ -17,9 +17,8 @@ limitations under the License.
package net package net
import ( import (
"errors"
"net" "net"
"net/url"
"os"
"reflect" "reflect"
"syscall" "syscall"
) )
@ -40,34 +39,18 @@ func IPNetEqual(ipnet1, ipnet2 *net.IPNet) bool {
// Returns if the given err is "connection reset by peer" error. // Returns if the given err is "connection reset by peer" error.
func IsConnectionReset(err error) bool { func IsConnectionReset(err error) bool {
if urlErr, ok := err.(*url.Error); ok { var errno syscall.Errno
err = urlErr.Err if errors.As(err, &errno) {
} return errno == syscall.ECONNRESET
if opErr, ok := err.(*net.OpError); ok {
err = opErr.Err
}
if osErr, ok := err.(*os.SyscallError); ok {
err = osErr.Err
}
if errno, ok := err.(syscall.Errno); ok && errno == syscall.ECONNRESET {
return true
} }
return false return false
} }
// Returns if the given err is "connection refused" error // Returns if the given err is "connection refused" error
func IsConnectionRefused(err error) bool { func IsConnectionRefused(err error) bool {
if urlErr, ok := err.(*url.Error); ok { var errno syscall.Errno
err = urlErr.Err if errors.As(err, &errno) {
} return errno == syscall.ECONNREFUSED
if opErr, ok := err.(*net.OpError); ok {
err = opErr.Err
}
if osErr, ok := err.(*os.SyscallError); ok {
err = osErr.Err
}
if errno, ok := err.(syscall.Errno); ok && errno == syscall.ECONNREFUSED {
return true
} }
return false return false
} }

View File

@ -19,8 +19,6 @@ package memory
import ( import (
"errors" "errors"
"fmt" "fmt"
"net"
"net/url"
"sync" "sync"
"syscall" "syscall"
@ -64,20 +62,12 @@ var _ discovery.CachedDiscoveryInterface = &memCacheClient{}
// "Connection reset" error which usually means that apiserver is temporarily // "Connection reset" error which usually means that apiserver is temporarily
// unavailable. // unavailable.
func isTransientConnectionError(err error) bool { func isTransientConnectionError(err error) bool {
urlError, ok := err.(*url.Error) var errno syscall.Errno
if !ok { if errors.As(err, &errno) {
return false
}
opError, ok := urlError.Err.(*net.OpError)
if !ok {
return false
}
errno, ok := opError.Err.(syscall.Errno)
if !ok {
return false
}
return errno == syscall.ECONNREFUSED || errno == syscall.ECONNRESET return errno == syscall.ECONNREFUSED || errno == syscall.ECONNRESET
} }
return false
}
func isTransientError(err error) bool { func isTransientError(err error) bool {
if isTransientConnectionError(err) { if isTransientConnectionError(err) {