OLD | NEW |
1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
4 | 4 |
5 package errors | 5 package errors |
6 | 6 |
7 import ( | 7 import ( |
8 "errors" | 8 "errors" |
9 "fmt" | 9 "fmt" |
10 "testing" | 10 "testing" |
11 | 11 |
12 . "github.com/smartystreets/goconvey/convey" | 12 . "github.com/smartystreets/goconvey/convey" |
13 ) | 13 ) |
14 | 14 |
15 type otherMEType []error | 15 type otherMEType []error |
16 | 16 |
17 func (o otherMEType) Error() string { return "FAIL" } | 17 func (o otherMEType) Error() string { return "FAIL" } |
18 | 18 |
19 func TestMultiError(t *testing.T) { | 19 func TestMultiError(t *testing.T) { |
20 t.Parallel() | 20 t.Parallel() |
21 | 21 |
22 Convey("MultiError works", t, func() { | 22 Convey("MultiError works", t, func() { |
23 var me error = MultiError{fmt.Errorf("hello"), fmt.Errorf("bob")
} | 23 var me error = MultiError{fmt.Errorf("hello"), fmt.Errorf("bob")
} |
24 | 24 |
25 So(me.Error(), ShouldEqual, `hello (and 1 other error)`) | 25 So(me.Error(), ShouldEqual, `hello (and 1 other error)`) |
26 | |
27 Convey("MultiErrorFromErrors with errors works", func() { | |
28 mec := make(chan error, 4) | |
29 mec <- nil | |
30 mec <- fmt.Errorf("first error") | |
31 mec <- nil | |
32 mec <- fmt.Errorf("what") | |
33 close(mec) | |
34 | |
35 err := MultiErrorFromErrors(mec) | |
36 So(err.Error(), ShouldEqual, `first error (and 1 other e
rror)`) | |
37 }) | |
38 | |
39 Convey("MultiErrorFromErrors with nil works", func() { | |
40 So(MultiErrorFromErrors(nil), ShouldBeNil) | |
41 | |
42 c := make(chan error) | |
43 close(c) | |
44 So(MultiErrorFromErrors(c), ShouldBeNil) | |
45 | |
46 c = make(chan error, 2) | |
47 c <- nil | |
48 c <- nil | |
49 close(c) | |
50 So(MultiErrorFromErrors(c), ShouldBeNil) | |
51 }) | |
52 }) | 26 }) |
53 } | 27 } |
54 | 28 |
55 func TestUpstreamErrors(t *testing.T) { | 29 func TestUpstreamErrors(t *testing.T) { |
56 t.Parallel() | 30 t.Parallel() |
57 | 31 |
58 Convey("Test MultiError", t, func() { | 32 Convey("Test MultiError", t, func() { |
59 Convey("nil", func() { | 33 Convey("nil", func() { |
60 me := MultiError(nil) | 34 me := MultiError(nil) |
61 So(me.Error(), ShouldEqual, "(0 errors)") | 35 So(me.Error(), ShouldEqual, "(0 errors)") |
(...skipping 16 matching lines...) Expand all Loading... |
78 Convey("single", func() { | 52 Convey("single", func() { |
79 So(SingleError(error(me)), ShouldResemble, error
s.New("sup")) | 53 So(SingleError(error(me)), ShouldResemble, error
s.New("sup")) |
80 }) | 54 }) |
81 }) | 55 }) |
82 }) | 56 }) |
83 | 57 |
84 Convey("SingleError passes through", t, func() { | 58 Convey("SingleError passes through", t, func() { |
85 e := errors.New("unique") | 59 e := errors.New("unique") |
86 So(SingleError(e), ShouldEqual, e) | 60 So(SingleError(e), ShouldEqual, e) |
87 }) | 61 }) |
88 | |
89 Convey("Test MultiError Conversion", t, func() { | |
90 ome := otherMEType{errors.New("sup")} | |
91 So(Fix(ome), ShouldHaveSameTypeAs, MultiError{}) | |
92 }) | |
93 | |
94 Convey("Fix passes through", t, func() { | |
95 e := errors.New("unique") | |
96 So(Fix(e), ShouldEqual, e) | |
97 }) | |
98 } | 62 } |
99 | |
100 func ExampleMultiError() { | |
101 errCh := make(chan error, 10) | |
102 errCh <- nil // nils are ignored | |
103 errCh <- fmt.Errorf("what") | |
104 close(errCh) | |
105 | |
106 err := MultiErrorFromErrors(errCh) | |
107 fmt.Printf("got: %s len=%d\n", err, len(err.(MultiError))) | |
108 | |
109 errCh = make(chan error, 10) | |
110 errCh <- nil // and if the channel only has nils | |
111 close(errCh) | |
112 | |
113 err = MultiErrorFromErrors(errCh) // then it returns nil | |
114 fmt.Printf("got: %v\n", err) | |
115 | |
116 // Output: | |
117 // got: what len=1 | |
118 // got: <nil> | |
119 } | |
OLD | NEW |