Files
web_huangguo_server/pub/common/srvdef/srvdef.go
T
rootandClaude Opus 5.5 7974a42301 Initial commit
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-23 11:07:42 +08:00

68 lines
1.4 KiB
Go

package srvdef
import (
"fmt"
)
// SrvErr 代替/common/error中的定义,逐渐在代码中弃掉(common/error模块)
type SrvErr struct {
Code CodeType `json:"code"`
Msg string `json:"msg"`
Tips string `json:"tips"`
}
// New create an error
func New(code CodeType, msgAndTips ...string) *SrvErr {
msg := code.Msg()
tips := code.Tip()
if len(msgAndTips) == 1 {
msg = msgAndTips[0]
} else if len(msgAndTips) == 2 {
msg, tips = msgAndTips[0], msgAndTips[1]
}
return &SrvErr{
Code: code,
Msg: msg,
Tips: tips,
}
}
// SrvErr 返回字符串
func (s *SrvErr) Error() string {
if s == nil {
return ""
}
return fmt.Sprintf("code = %d, msg = %s ,tips = %v", s.Code, s.Msg, s.Tips)
}
// SrvErr 返回error
func (s *SrvErr) ToError() error {
if s == nil {
return nil
}
return fmt.Errorf("code = %d, msg = %s ,tips = %v", s.Code, s.Msg, s.Tips)
}
func (c *SrvErr) GetMsg() string {
// 有自定义的msg就用自定义的msg
if c.Msg != "" {
return c.Msg
}
return c.Code.Msg()
}
func (c *SrvErr) GetTip() string {
// 有自定义的tip就用自定义的tip
if c.Tips != "" {
return c.Tips
}
return c.Code.Tip()
}
func (c *SrvErr) GetCode() CodeType { return c.Code }
var (
ErrDbInsert = New(ErrDbInsertError, "", "")
ErrDbQuery = New(ErrDbQueryError, "", "")
ErrDbUpdate = New(ErrDbUpdateError, "", "")
ErrDbDelete = New(ErrDbDeleteError, "", "")
)