Skip to content

Commit 705b23a

Browse files
authored
Merge pull request #2 from starboard-nz/feature/reset
implement Reset()
2 parents c5b889c + e5fea52 commit 705b23a

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

shared_error.go

+24
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ func (s *SharedError) Error() string {
3030
s.lock.Lock()
3131
defer s.lock.Unlock()
3232

33+
return s.string()
34+
}
35+
36+
// assumes that s is already locked
37+
func (s *SharedError) string() string {
3338
if len(s.err) == 0 {
3439
return ""
3540
}
@@ -75,6 +80,25 @@ func (s *SharedError) Store(err error) {
7580
s.err = append(s.err, err)
7681
}
7782

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+
78102
// Store stores an error condition in SharedError.
79103
func (s *SharedError) Storef(format string, args ...interface{}) {
80104
if format == "" {

shared_error_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,25 @@ func TestSharedErrorIsAll(t *testing.T) {
123123
t.Errorf("expected not all errors match on target error, got all matches")
124124
}
125125
}
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+
}

0 commit comments

Comments
 (0)