aboutsummaryrefslogtreecommitdiff
path: root/tun/errors.go
blob: e70b13c337fd8aa58d338459a344a365e2cdcbc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package tun

import (
	"errors"
	"fmt"
)

var (
	// ErrTooManySegments is returned by Device.Read() when segmentation
	// overflows the length of supplied buffers. This error should not cause
	// 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]
}