]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
tun: replace ErrorBatch() with errors.Join()
authorJordan Whited <jordan@tailscale.com>
Thu, 16 Mar 2023 20:27:51 +0000 (13:27 -0700)
committerJason A. Donenfeld <Jason@zx2c4.com>
Fri, 17 Mar 2023 14:18:04 +0000 (15:18 +0100)
Reviewed-by: Maisem Ali <maisem@tailscale.com>
Signed-off-by: Jordan Whited <jordan@tailscale.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
tun/errors.go
tun/tun_linux.go

index e70b13c337fd8aa58d338459a344a365e2cdcbc3..75ae3a434a7d53fe71dcb713a473077eaaf14499 100644 (file)
@@ -2,7 +2,6 @@ package tun
 
 import (
        "errors"
-       "fmt"
 )
 
 var (
@@ -11,50 +10,3 @@ var (
        // reads to cease.
        ErrTooManySegments = errors.New("too many segments")
 )
-
-type errorBatch []error
-
-// ErrorBatch takes a possibly nil or empty list of errors, and if the list is
-// non-nil returns an error type that wraps all of the errors. Expected usage is
-// to append to an []errors and coerce the set to an error using this method.
-func ErrorBatch(errs []error) error {
-       if len(errs) == 0 {
-               return nil
-       }
-       return errorBatch(errs)
-}
-
-func (e errorBatch) Error() string {
-       if len(e) == 0 {
-               return ""
-       }
-       if len(e) == 1 {
-               return e[0].Error()
-       }
-       return fmt.Sprintf("batch operation: %v (and %d more errors)", e[0], len(e)-1)
-}
-
-func (e errorBatch) Is(target error) bool {
-       for _, err := range e {
-               if errors.Is(err, target) {
-                       return true
-               }
-       }
-       return false
-}
-
-func (e errorBatch) As(target interface{}) bool {
-       for _, err := range e {
-               if errors.As(err, target) {
-                       return true
-               }
-       }
-       return false
-}
-
-func (e errorBatch) Unwrap() error {
-       if len(e) == 0 {
-               return nil
-       }
-       return e[0]
-}
index 7a96d47f0afaedeaae814903271f44370647883b..12cd49f747dc120e84fb208d780da31a39121dfd 100644 (file)
@@ -338,7 +338,7 @@ func (tun *NativeTun) Write(bufs [][]byte, offset int) (int, error) {
                tun.writeOpMu.Unlock()
        }()
        var (
-               errs  []error
+               errs  error
                total int
        )
        tun.toWrite = tun.toWrite[:0]
@@ -359,12 +359,12 @@ func (tun *NativeTun) Write(bufs [][]byte, offset int) (int, error) {
                        return total, os.ErrClosed
                }
                if err != nil {
-                       errs = append(errs, err)
+                       errs = errors.Join(errs, err)
                } else {
                        total += n
                }
        }
-       return total, ErrorBatch(errs)
+       return total, errs
 }
 
 // handleVirtioRead splits in into bufs, leaving offset bytes at the front of