Skip to content

Commit 8387894

Browse files
committed
initial import
0 parents  commit 8387894

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

LICENSE

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2021 Xerra Earth Observation Institute
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## Docs coming soon...

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module gitlab.com/xerra/common/sharederror
2+
3+
go 1.15

shared_error.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Copyright (c) 2021, Xerra Earth Observation Institute
3+
* All rights reserved. Use is subject to License terms.
4+
* See LICENSE.TXT in the root directory of this source tree.
5+
*/
6+
7+
package sharederror
8+
9+
import (
10+
"fmt"
11+
"sync"
12+
)
13+
14+
// SharedError implements goroutine-safe error handling.
15+
// Multiple concurrent goroutines can share a SharedError variable to reports errors
16+
// to the calling function.
17+
type SharedError struct {
18+
err []error
19+
lock sync.Mutex
20+
}
21+
22+
// Error implements the error interface, you can return a SharedError as an error.
23+
func (s *SharedError) Error() string {
24+
s.lock.Lock()
25+
defer s.lock.Unlock()
26+
27+
if len(s.err) == 0 {
28+
return ""
29+
}
30+
31+
if len(s.err) == 1 {
32+
return s.err[0].Error()
33+
}
34+
35+
errorStr := ""
36+
37+
for i, err := range s.err {
38+
if i != 0 {
39+
errorStr += " / "
40+
}
41+
42+
errorStr += fmt.Sprintf("error %d: %v", i, err)
43+
}
44+
45+
return errorStr
46+
}
47+
48+
// Triggered returns true if an error is stored in the ShareError, or false for no error.
49+
func (s *SharedError) Triggered() bool {
50+
s.lock.Lock()
51+
defer s.lock.Unlock()
52+
53+
if len(s.err) == 0 {
54+
return false
55+
}
56+
57+
return true
58+
}
59+
60+
// Store stores an error condition in SharedError.
61+
func (s *SharedError) Store(err error) {
62+
if err == nil {
63+
return
64+
}
65+
66+
s.lock.Lock()
67+
defer s.lock.Unlock()
68+
69+
s.err = append(s.err, err)
70+
}

0 commit comments

Comments
 (0)