File tree 2 files changed +46
-0
lines changed
2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -30,6 +30,11 @@ func (s *SharedError) Error() string {
30
30
s .lock .Lock ()
31
31
defer s .lock .Unlock ()
32
32
33
+ return s .string ()
34
+ }
35
+
36
+ // assumes that s is already locked
37
+ func (s * SharedError ) string () string {
33
38
if len (s .err ) == 0 {
34
39
return ""
35
40
}
@@ -75,6 +80,25 @@ func (s *SharedError) Store(err error) {
75
80
s .err = append (s .err , err )
76
81
}
77
82
83
+ // Return any error and reset the error if it was triggered.
84
+ func (s * SharedError ) Reset () error {
85
+ if s == nil {
86
+ return nil
87
+ }
88
+
89
+ s .lock .Lock ()
90
+ defer s .lock .Unlock ()
91
+
92
+ err := s .string ()
93
+ s .err = nil
94
+
95
+ if err == "" {
96
+ return nil
97
+ }
98
+
99
+ return errors .New (err )
100
+ }
101
+
78
102
// Store stores an error condition in SharedError.
79
103
func (s * SharedError ) Storef (format string , args ... interface {}) {
80
104
if format == "" {
Original file line number Diff line number Diff line change @@ -123,3 +123,25 @@ func TestSharedErrorIsAll(t *testing.T) {
123
123
t .Errorf ("expected not all errors match on target error, got all matches" )
124
124
}
125
125
}
126
+
127
+ func TestReset (t * testing.T ) {
128
+ var sharedErr = sharederror .NewSharedError ()
129
+
130
+ err := sharedErr .Reset ()
131
+ if err != nil {
132
+ t .Errorf ("incorrect result" )
133
+ }
134
+
135
+ t .Log ("store error" )
136
+ sharedErr .Store (fmt .Errorf ("some error" ))
137
+
138
+ err = sharedErr .Reset ()
139
+ if err == nil {
140
+ t .Errorf ("incorrect result" )
141
+ }
142
+
143
+ err = sharedErr .Reset ()
144
+ if err != nil {
145
+ t .Errorf ("incorrect result" )
146
+ }
147
+ }
You can’t perform that action at this time.
0 commit comments