Skip to content

Commit 1417a47

Browse files
jwhitedzx2c4
authored andcommitted
tun: replace ErrorBatch() with errors.Join()
Reviewed-by: Maisem Ali <[email protected]> Signed-off-by: Jordan Whited <[email protected]> Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent 7f511c3 commit 1417a47

File tree

2 files changed

+3
-51
lines changed

2 files changed

+3
-51
lines changed

tun/errors.go

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package tun
22

33
import (
44
"errors"
5-
"fmt"
65
)
76

87
var (
@@ -11,50 +10,3 @@ var (
1110
// reads to cease.
1211
ErrTooManySegments = errors.New("too many segments")
1312
)
14-
15-
type errorBatch []error
16-
17-
// ErrorBatch takes a possibly nil or empty list of errors, and if the list is
18-
// non-nil returns an error type that wraps all of the errors. Expected usage is
19-
// to append to an []errors and coerce the set to an error using this method.
20-
func ErrorBatch(errs []error) error {
21-
if len(errs) == 0 {
22-
return nil
23-
}
24-
return errorBatch(errs)
25-
}
26-
27-
func (e errorBatch) Error() string {
28-
if len(e) == 0 {
29-
return ""
30-
}
31-
if len(e) == 1 {
32-
return e[0].Error()
33-
}
34-
return fmt.Sprintf("batch operation: %v (and %d more errors)", e[0], len(e)-1)
35-
}
36-
37-
func (e errorBatch) Is(target error) bool {
38-
for _, err := range e {
39-
if errors.Is(err, target) {
40-
return true
41-
}
42-
}
43-
return false
44-
}
45-
46-
func (e errorBatch) As(target interface{}) bool {
47-
for _, err := range e {
48-
if errors.As(err, target) {
49-
return true
50-
}
51-
}
52-
return false
53-
}
54-
55-
func (e errorBatch) Unwrap() error {
56-
if len(e) == 0 {
57-
return nil
58-
}
59-
return e[0]
60-
}

tun/tun_linux.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ func (tun *NativeTun) Write(bufs [][]byte, offset int) (int, error) {
338338
tun.writeOpMu.Unlock()
339339
}()
340340
var (
341-
errs []error
341+
errs error
342342
total int
343343
)
344344
tun.toWrite = tun.toWrite[:0]
@@ -359,12 +359,12 @@ func (tun *NativeTun) Write(bufs [][]byte, offset int) (int, error) {
359359
return total, os.ErrClosed
360360
}
361361
if err != nil {
362-
errs = append(errs, err)
362+
errs = errors.Join(errs, err)
363363
} else {
364364
total += n
365365
}
366366
}
367-
return total, ErrorBatch(errs)
367+
return total, errs
368368
}
369369

370370
// handleVirtioRead splits in into bufs, leaving offset bytes at the front of

0 commit comments

Comments
 (0)