-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
executable file
·48 lines (41 loc) · 1.02 KB
/
registry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package errors
import (
"fmt"
"os"
"sync"
"github.com/coinbase/rosetta-sdk-go/types"
)
type errorRegistry struct {
mu *sync.RWMutex
sealed bool
errors map[int32]*types.Error
}
func (r *errorRegistry) add(err *Error) {
r.mu.Lock()
defer r.mu.Unlock()
if r.sealed {
_, _ = fmt.Fprintln(os.Stderr, "[ROSETTA] WARNING: attempts to register errors after seal will be ignored")
}
if _, ok := r.errors[err.rosErr.Code]; ok {
_, _ = fmt.Fprintln(os.Stderr, "[ROSETTA] WARNING: attempts to register an already registered error will be ignored, code: ", err.rosErr.Code)
}
r.errors[err.rosErr.Code] = err.rosErr
}
func (r errorRegistry) list() []*types.Error {
r.mu.RLock()
defer r.mu.RUnlock()
rosErrs := make([]*types.Error, 0, len(registry.errors))
for _, v := range r.errors {
rosErrs = append(rosErrs, v)
}
return rosErrs
}
func (r *errorRegistry) seal() {
r.mu.Lock()
defer r.mu.Unlock()
r.sealed = true
}
var registry = errorRegistry{
mu: new(sync.RWMutex),
errors: make(map[int32]*types.Error),
}