@@ -0,0 +1,47 @@
|
||||
package stderr
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Code int
|
||||
|
||||
type CodeMsg struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Tip string `json:"tip,omitempty"`
|
||||
}
|
||||
|
||||
func (c Code) Msg() string {
|
||||
msg, ok := codeAndMsg[c]
|
||||
if ok {
|
||||
return msg
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (c Code) Tip() string {
|
||||
tip, ok := codeAndTip[c]
|
||||
if ok {
|
||||
return tip
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (c Code) Error() string {
|
||||
return fmt.Sprintf("code=%d,msg=%v,tip=%v", c, c.Msg(), c.Tip())
|
||||
}
|
||||
|
||||
func (c Code) Struct() CodeMsg {
|
||||
return CodeMsg{Code: int(c), Msg: c.Msg(), Tip: c.Tip()}
|
||||
}
|
||||
|
||||
func New(code int, msg string) error {
|
||||
return fmt.Errorf("code:%d,msg:%s", code, msg)
|
||||
}
|
||||
|
||||
func (c Code) SetMsg(msg string) CodeMsg {
|
||||
return CodeMsg{Code: int(c), Msg: msg, Tip: c.Tip()}
|
||||
}
|
||||
|
||||
func (c Code) SetTip(tip string) CodeMsg {
|
||||
return CodeMsg{Code: int(c), Msg: c.Msg(), Tip: tip}
|
||||
}
|
||||
Reference in New Issue
Block a user