...

Source file src/go.uber.org/multierr/example_test.go

Documentation: go.uber.org/multierr

     1  // Copyright (c) 2017-2021 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package multierr_test
    22  
    23  import (
    24  	"errors"
    25  	"fmt"
    26  	"io"
    27  
    28  	"go.uber.org/multierr"
    29  )
    30  
    31  func ExampleCombine() {
    32  	err := multierr.Combine(
    33  		errors.New("call 1 failed"),
    34  		nil, // successful request
    35  		errors.New("call 3 failed"),
    36  		nil, // successful request
    37  		errors.New("call 5 failed"),
    38  	)
    39  	fmt.Printf("%+v", err)
    40  	// Output:
    41  	// the following errors occurred:
    42  	//  -  call 1 failed
    43  	//  -  call 3 failed
    44  	//  -  call 5 failed
    45  }
    46  
    47  func ExampleAppend() {
    48  	var err error
    49  	err = multierr.Append(err, errors.New("call 1 failed"))
    50  	err = multierr.Append(err, errors.New("call 2 failed"))
    51  	fmt.Println(err)
    52  	// Output:
    53  	// call 1 failed; call 2 failed
    54  }
    55  
    56  func ExampleErrors() {
    57  	err := multierr.Combine(
    58  		nil, // successful request
    59  		errors.New("call 2 failed"),
    60  		errors.New("call 3 failed"),
    61  	)
    62  	err = multierr.Append(err, nil) // successful request
    63  	err = multierr.Append(err, errors.New("call 5 failed"))
    64  
    65  	errors := multierr.Errors(err)
    66  	for _, err := range errors {
    67  		fmt.Println(err)
    68  	}
    69  	// Output:
    70  	// call 2 failed
    71  	// call 3 failed
    72  	// call 5 failed
    73  }
    74  
    75  func ExampleAppendInto() {
    76  	var err error
    77  
    78  	if multierr.AppendInto(&err, errors.New("foo")) {
    79  		fmt.Println("call 1 failed")
    80  	}
    81  
    82  	if multierr.AppendInto(&err, nil) {
    83  		fmt.Println("call 2 failed")
    84  	}
    85  
    86  	if multierr.AppendInto(&err, errors.New("baz")) {
    87  		fmt.Println("call 3 failed")
    88  	}
    89  
    90  	fmt.Println(err)
    91  	// Output:
    92  	// call 1 failed
    93  	// call 3 failed
    94  	// foo; baz
    95  }
    96  
    97  type fakeCloser func() error
    98  
    99  func (f fakeCloser) Close() error {
   100  	return f()
   101  }
   102  
   103  func FakeCloser(err error) io.Closer {
   104  	return fakeCloser(func() error {
   105  		return err
   106  	})
   107  }
   108  
   109  func ExampleClose() {
   110  	var err error
   111  
   112  	closer := FakeCloser(errors.New("foo"))
   113  
   114  	defer func() {
   115  		fmt.Println(err)
   116  	}()
   117  	defer multierr.AppendInvoke(&err, multierr.Close(closer))
   118  
   119  	fmt.Println("Hello, World")
   120  
   121  	// Output:
   122  	// Hello, World
   123  	// foo
   124  }
   125  

View as plain text