Adding upstream version 0.0~git20250501.cd50c6a.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
947813c282
commit
d8f2a7c92a
16 changed files with 3363 additions and 0 deletions
44
join.go
Normal file
44
join.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package errors
|
||||
|
||||
import "unsafe"
|
||||
|
||||
func Join(errs ...error) error {
|
||||
n := 0
|
||||
for _, err := range errs {
|
||||
if err != nil {
|
||||
n++
|
||||
}
|
||||
}
|
||||
if n == 0 {
|
||||
return nil
|
||||
}
|
||||
e := &joinError{
|
||||
errs: make([]error, 0, n),
|
||||
}
|
||||
for _, err := range errs {
|
||||
if err != nil {
|
||||
e.errs = append(e.errs, err)
|
||||
}
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
type joinError struct {
|
||||
errs []error
|
||||
}
|
||||
|
||||
func (e *joinError) Error() string {
|
||||
// Since Join returns nil if every value in errs is nil,
|
||||
// e.errs cannot be empty.
|
||||
if len(e.errs) == 1 {
|
||||
return e.errs[0].Error()
|
||||
}
|
||||
|
||||
b := []byte(e.errs[0].Error())
|
||||
for _, err := range e.errs[1:] {
|
||||
b = append(b, ':', ' ')
|
||||
b = append(b, err.Error()...)
|
||||
}
|
||||
// At this point, b has at least one byte '\n'.
|
||||
return unsafe.String(&b[0], len(b))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue