|  | 
|  | 1 | +/* | 
|  | 2 | +   Copyright © 2022 The CDI Authors | 
|  | 3 | +
 | 
|  | 4 | +   Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | +   you may not use this file except in compliance with the License. | 
|  | 6 | +   You may obtain a copy of the License at | 
|  | 7 | +
 | 
|  | 8 | +       http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | +
 | 
|  | 10 | +   Unless required by applicable law or agreed to in writing, software | 
|  | 11 | +   distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | +   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | +   See the License for the specific language governing permissions and | 
|  | 14 | +   limitations under the License. | 
|  | 15 | +*/ | 
|  | 16 | + | 
|  | 17 | +package multierror | 
|  | 18 | + | 
|  | 19 | +import ( | 
|  | 20 | +	"strings" | 
|  | 21 | +) | 
|  | 22 | + | 
|  | 23 | +// New combines several errors into a single error. Parameters that are nil are | 
|  | 24 | +// ignored. If no errors are passed in or all parameters are nil, then the | 
|  | 25 | +// result is also nil. | 
|  | 26 | +func New(errors ...error) error { | 
|  | 27 | +	// Filter out nil entries. | 
|  | 28 | +	numErrors := 0 | 
|  | 29 | +	for _, err := range errors { | 
|  | 30 | +		if err != nil { | 
|  | 31 | +			errors[numErrors] = err | 
|  | 32 | +			numErrors++ | 
|  | 33 | +		} | 
|  | 34 | +	} | 
|  | 35 | +	if numErrors == 0 { | 
|  | 36 | +		return nil | 
|  | 37 | +	} | 
|  | 38 | +	return multiError(errors[0:numErrors]) | 
|  | 39 | +} | 
|  | 40 | + | 
|  | 41 | +// multiError is the underlying implementation used by New. | 
|  | 42 | +// | 
|  | 43 | +// Beware that a null multiError is not the same as a nil error. | 
|  | 44 | +type multiError []error | 
|  | 45 | + | 
|  | 46 | +// multiError returns all individual error strings concatenated with "\n" | 
|  | 47 | +func (e multiError) Error() string { | 
|  | 48 | +	var builder strings.Builder | 
|  | 49 | +	for i, err := range e { | 
|  | 50 | +		if i > 0 { | 
|  | 51 | +			_, _ = builder.WriteString("\n") | 
|  | 52 | +		} | 
|  | 53 | +		_, _ = builder.WriteString(err.Error()) | 
|  | 54 | +	} | 
|  | 55 | +	return builder.String() | 
|  | 56 | +} | 
|  | 57 | + | 
|  | 58 | +// Append returns a new multi error all errors concatenated. Errors that are | 
|  | 59 | +// multi errors get flattened, nil is ignored. | 
|  | 60 | +func Append(err error, errors ...error) error { | 
|  | 61 | +	var result multiError | 
|  | 62 | +	if m, ok := err.(multiError); ok { | 
|  | 63 | +		result = m | 
|  | 64 | +	} else if err != nil { | 
|  | 65 | +		result = append(result, err) | 
|  | 66 | +	} | 
|  | 67 | + | 
|  | 68 | +	for _, e := range errors { | 
|  | 69 | +		if e == nil { | 
|  | 70 | +			continue | 
|  | 71 | +		} | 
|  | 72 | +		if m, ok := e.(multiError); ok { | 
|  | 73 | +			result = append(result, m...) | 
|  | 74 | +		} else { | 
|  | 75 | +			result = append(result, e) | 
|  | 76 | +		} | 
|  | 77 | +	} | 
|  | 78 | +	if len(result) == 0 { | 
|  | 79 | +		return nil | 
|  | 80 | +	} | 
|  | 81 | +	return result | 
|  | 82 | +} | 
0 commit comments