-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patherrorset.go
More file actions
85 lines (71 loc) · 2.04 KB
/
Copy patherrorset.go
File metadata and controls
85 lines (71 loc) · 2.04 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company
// SPDX-License-Identifier: Apache-2.0
package errext
import (
"fmt"
"os"
"strings"
"github.com/sapcc/go-bits/logg"
)
// ErrorSet replaces the "error" return value in functions that can return
// multiple errors. It provides convenience functions for easily adding errors
// to the set.
type ErrorSet []error
// Add adds the given error to the set if it is non-nil.
func (errs *ErrorSet) Add(err error) {
if err != nil {
*errs = append(*errs, err)
}
}
// Addf is a shorthand for errs.Add(fmt.Errorf(...)).
func (errs *ErrorSet) Addf(msg string, args ...any) {
*errs = append(*errs, fmt.Errorf(msg, args...))
}
// Append adds all errors from the `other` ErrorSet to this one.
func (errs *ErrorSet) Append(other ErrorSet) {
*errs = append(*errs, other...)
}
// IsEmpty returns true if no errors are in the set.
func (errs ErrorSet) IsEmpty() bool {
return len(errs) == 0
}
// Join joins the messages of all errors in this set using the provided separator.
// If the set is empty, an empty string is returned.
func (errs ErrorSet) Join(sep string) string {
return errs.JoinedError(sep).Error()
}
// JoinedError is like Join, but returns an error that can be unwrapped.
func (errs ErrorSet) JoinedError(sep string) error {
return joinedError{[]error(errs), sep}
}
// LogFatalIfError reports all errors in this set on level FATAL, thus dying if
// there are any errors.
func (errs ErrorSet) LogFatalIfError() {
hasErrors := false
for _, err := range errs {
hasErrors = true
logg.Other("FATAL", err.Error())
}
if hasErrors {
os.Exit(1)
}
}
type joinedError struct {
errs []error
separator string
}
// Error implements the builtin/error interface.
func (e joinedError) Error() string {
if len(e.errs) == 0 {
return ""
}
msgs := make([]string, len(e.errs))
for idx, err := range e.errs {
msgs[idx] = err.Error()
}
return strings.Join(msgs, e.separator)
}
// Unwrap implements the interface implied by package errors.
func (e joinedError) Unwrap() []error {
return e.errs
}