example_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. package multierr_test
  21. import (
  22. "errors"
  23. "fmt"
  24. "io"
  25. "go.uber.org/multierr"
  26. )
  27. func ExampleCombine() {
  28. err := multierr.Combine(
  29. errors.New("call 1 failed"),
  30. nil, // successful request
  31. errors.New("call 3 failed"),
  32. nil, // successful request
  33. errors.New("call 5 failed"),
  34. )
  35. fmt.Printf("%+v", err)
  36. // Output:
  37. // the following errors occurred:
  38. // - call 1 failed
  39. // - call 3 failed
  40. // - call 5 failed
  41. }
  42. func ExampleAppend() {
  43. var err error
  44. err = multierr.Append(err, errors.New("call 1 failed"))
  45. err = multierr.Append(err, errors.New("call 2 failed"))
  46. fmt.Println(err)
  47. // Output:
  48. // call 1 failed; call 2 failed
  49. }
  50. func ExampleErrors() {
  51. err := multierr.Combine(
  52. nil, // successful request
  53. errors.New("call 2 failed"),
  54. errors.New("call 3 failed"),
  55. )
  56. err = multierr.Append(err, nil) // successful request
  57. err = multierr.Append(err, errors.New("call 5 failed"))
  58. errors := multierr.Errors(err)
  59. for _, err := range errors {
  60. fmt.Println(err)
  61. }
  62. // Output:
  63. // call 2 failed
  64. // call 3 failed
  65. // call 5 failed
  66. }
  67. func ExampleAppendInto() {
  68. var err error
  69. if multierr.AppendInto(&err, errors.New("foo")) {
  70. fmt.Println("call 1 failed")
  71. }
  72. if multierr.AppendInto(&err, nil) {
  73. fmt.Println("call 2 failed")
  74. }
  75. if multierr.AppendInto(&err, errors.New("baz")) {
  76. fmt.Println("call 3 failed")
  77. }
  78. fmt.Println(err)
  79. // Output:
  80. // call 1 failed
  81. // call 3 failed
  82. // foo; baz
  83. }
  84. type fakeCloser func() error
  85. func (f fakeCloser) Close() error {
  86. return f()
  87. }
  88. func FakeCloser(err error) io.Closer {
  89. return fakeCloser(func() error {
  90. return err
  91. })
  92. }
  93. func ExampleClose() {
  94. var err error
  95. closer := FakeCloser(errors.New("foo"))
  96. defer func() {
  97. fmt.Println(err)
  98. }()
  99. defer multierr.AppendInvoke(&err, multierr.Close(closer))
  100. fmt.Println("Hello, World")
  101. // Output:
  102. // Hello, World
  103. // foo
  104. }