github.com/uber/kraken@v0.1.4/utils/errutil/errutil.go (about)

     1  // Copyright (c) 2016-2019 Uber Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package errutil
    15  
    16  import "bytes"
    17  
    18  // MultiError defines a list of multiple errors. Useful for type assertions and
    19  // inspecting individual errors.
    20  //
    21  // XXX: Don't initialize errors as MultiError unless you know what you're doing!
    22  // See https://golang.org/doc/faq#nil_error for more details.
    23  type MultiError []error
    24  
    25  func (e MultiError) Error() string {
    26  	var b bytes.Buffer
    27  	for i, err := range e {
    28  		b.WriteString(err.Error())
    29  		if i < len(e)-1 {
    30  			b.WriteString(", ")
    31  		}
    32  	}
    33  	return b.String()
    34  }
    35  
    36  // Join converts errs into an error interface.
    37  func Join(errs []error) error {
    38  	if errs == nil {
    39  		return nil
    40  	}
    41  	return MultiError(errs)
    42  }