center.go 694 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package fserr
  2. import (
  3. "go.uber.org/zap"
  4. "net/http"
  5. "sync"
  6. )
  7. var codeMap sync.Map
  8. var logger *zap.Logger
  9. var defaultErrCode = ErrCode{
  10. HttpCode: http.StatusOK,
  11. }
  12. func init() {
  13. var err error
  14. logger, err = zap.NewProduction()
  15. if err != nil {
  16. panic(err)
  17. }
  18. }
  19. func register(code ErrCode) {
  20. if _, ok := codeMap.Load(code.BusinessCode); ok {
  21. logger.Warn("duplicate business code", zap.Int("business", code.BusinessCode),
  22. zap.String("message", code.Message))
  23. }
  24. codeMap.Store(code.BusinessCode, code)
  25. }
  26. func getCode(business int) ErrCode {
  27. code, ok := codeMap.Load(business)
  28. if ok {
  29. return code.(ErrCode)
  30. }
  31. ret := defaultErrCode
  32. ret.BusinessCode = business
  33. return ret
  34. }