example_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (c) 2017 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. "go.uber.org/multierr"
  25. )
  26. func ExampleCombine() {
  27. err := multierr.Combine(
  28. errors.New("call 1 failed"),
  29. nil, // successful request
  30. errors.New("call 3 failed"),
  31. nil, // successful request
  32. errors.New("call 5 failed"),
  33. )
  34. fmt.Printf("%+v", err)
  35. // Output:
  36. // the following errors occurred:
  37. // - call 1 failed
  38. // - call 3 failed
  39. // - call 5 failed
  40. }
  41. func ExampleAppend() {
  42. var err error
  43. err = multierr.Append(err, errors.New("call 1 failed"))
  44. err = multierr.Append(err, errors.New("call 2 failed"))
  45. fmt.Println(err)
  46. // Output:
  47. // call 1 failed; call 2 failed
  48. }
  49. func ExampleErrors() {
  50. err := multierr.Combine(
  51. nil, // successful request
  52. errors.New("call 2 failed"),
  53. errors.New("call 3 failed"),
  54. )
  55. err = multierr.Append(err, nil) // successful request
  56. err = multierr.Append(err, errors.New("call 5 failed"))
  57. errors := multierr.Errors(err)
  58. for _, err := range errors {
  59. fmt.Println(err)
  60. }
  61. // Output:
  62. // call 2 failed
  63. // call 3 failed
  64. // call 5 failed
  65. }
  66. func ExampleAppendInto() {
  67. var err error
  68. if multierr.AppendInto(&err, errors.New("foo")) {
  69. fmt.Println("call 1 failed")
  70. }
  71. if multierr.AppendInto(&err, nil) {
  72. fmt.Println("call 2 failed")
  73. }
  74. if multierr.AppendInto(&err, errors.New("baz")) {
  75. fmt.Println("call 3 failed")
  76. }
  77. fmt.Println(err)
  78. // Output:
  79. // call 1 failed
  80. // call 3 failed
  81. // foo; baz
  82. }