1234567891011121314151617181920212223242526272829303132333435363738394041 |
- package fserr
- import (
- "go.uber.org/zap"
- "net/http"
- "sync"
- )
- var codeMap sync.Map
- var logger *zap.Logger
- var defaultErrCode = ErrCode{
- HttpCode: http.StatusOK,
- }
- func init() {
- var err error
- logger, err = zap.NewProduction()
- if err != nil {
- panic(err)
- }
- }
- func register(code ErrCode) {
- if _, ok := codeMap.Load(code.BusinessCode); ok {
- logger.Warn("duplicate business code", zap.Int("business", code.BusinessCode),
- zap.String("message", code.Message))
- }
- codeMap.Store(code.BusinessCode, code)
- }
- func getCode(business int) ErrCode {
- code, ok := codeMap.Load(business)
- if ok {
- return code.(ErrCode)
- }
- ret := defaultErrCode
- ret.BusinessCode = business
- return ret
- }
|