mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-20 23:41:00 -06:00
a9cff5b05e
Signed-off-by: Stephan Renatus <stephan@styra.com>
54 lines
1.0 KiB
Go
54 lines
1.0 KiB
Go
// Copyright 2022 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
//go:build !go1.20
|
|
|
|
package errors
|
|
|
|
// Join returns an error that wraps the given errors.
|
|
// Any nil error values are discarded.
|
|
// Join returns nil if errs contains no non-nil values.
|
|
// The error formats as the concatenation of the strings obtained
|
|
// by calling the Error method of each element of errs, with a newline
|
|
// between each string.
|
|
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 {
|
|
var b []byte
|
|
for i, err := range e.errs {
|
|
if i > 0 {
|
|
b = append(b, '\n')
|
|
}
|
|
b = append(b, err.Error()...)
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
func (e *joinError) Unwrap() []error {
|
|
return e.errs
|
|
}
|