center.go 541 B

123456789101112131415161718192021222324252627282930
  1. package fserr
  2. import (
  3. "fmt"
  4. "net/http"
  5. "sync"
  6. )
  7. var codeMap sync.Map
  8. var defaultErrCode = ErrCode{
  9. HttpCode: http.StatusOK,
  10. }
  11. func register(code ErrCode) {
  12. if _, ok := codeMap.Load(code.BusinessCode); ok {
  13. panic(fmt.Sprintf("duplicate business code, code: %d, message: %s", code.BusinessCode, code.Message))
  14. }
  15. codeMap.Store(code.BusinessCode, code)
  16. }
  17. func getCode(business int) ErrCode {
  18. code, ok := codeMap.Load(business)
  19. if ok {
  20. return code.(ErrCode)
  21. }
  22. ret := defaultErrCode
  23. ret.BusinessCode = business
  24. return ret
  25. }