github.com/Finschia/finschia-sdk@v0.48.1/server/rosetta/lib/errors/registry.go (about)

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