From f0363a7f6b626731b5c960482f26bd01c1b69fda Mon Sep 17 00:00:00 2001 From: Danny Canter Date: Mon, 6 May 2024 00:44:13 -0700 Subject: [PATCH] Chore: Simplify some syscall error checks This just replaces some type casts to check whether a few dial errors are a specific syscall with the stdlibs errors.As/errors.Is pals. Signed-off-by: Danny Canter --- pkg/dialer/dialer_unix.go | 13 ++----------- pkg/shim/util_unix.go | 23 ++++++++--------------- 2 files changed, 10 insertions(+), 26 deletions(-) diff --git a/pkg/dialer/dialer_unix.go b/pkg/dialer/dialer_unix.go index 798566bc5..1c58d16b6 100644 --- a/pkg/dialer/dialer_unix.go +++ b/pkg/dialer/dialer_unix.go @@ -19,9 +19,9 @@ package dialer import ( + "errors" "fmt" "net" - "os" "strings" "syscall" "time" @@ -34,16 +34,7 @@ func DialAddress(address string) string { } func isNoent(err error) bool { - if err != nil { - if nerr, ok := err.(*net.OpError); ok { - if serr, ok := nerr.Err.(*os.SyscallError); ok { - if serr.Err == syscall.ENOENT { - return true - } - } - } - } - return false + return errors.Is(err, syscall.ENOENT) } func dialer(address string, timeout time.Duration) (net.Conn, error) { diff --git a/pkg/shim/util_unix.go b/pkg/shim/util_unix.go index fc9becdb4..4a46e27c3 100644 --- a/pkg/shim/util_unix.go +++ b/pkg/shim/util_unix.go @@ -22,6 +22,7 @@ import ( "bufio" "context" "crypto/sha256" + "errors" "fmt" "io" "math" @@ -173,22 +174,14 @@ func RemoveSocket(address string) error { // SocketEaddrinuse returns true if the provided error is caused by the // EADDRINUSE error number func SocketEaddrinuse(err error) bool { - netErr, ok := err.(*net.OpError) - if !ok { - return false + var netErr *net.OpError + if errors.As(err, &netErr) { + if netErr.Op != "listen" { + return false + } + return errors.Is(err, syscall.EADDRINUSE) } - if netErr.Op != "listen" { - return false - } - syscallErr, ok := netErr.Err.(*os.SyscallError) - if !ok { - return false - } - errno, ok := syscallErr.Err.(syscall.Errno) - if !ok { - return false - } - return errno == syscall.EADDRINUSE + return false } // CanConnect returns true if the socket provided at the address