游戏服务器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

152 lines
3.7 KiB

package codes
import (
"fmt"
"io"
"strconv"
"strings"
)
var (
OK = NewCode(0, "ok")
Canceled = NewCode(1, "canceled")
Unknown = NewCode(2, "unknown")
InvalidArgument = NewCode(3, "invalid argument")
DeadlineExceeded = NewCode(4, "deadline exceeded")
NotFound = NewCode(5, "not found")
InternalError = NewCode(6, "internal error")
Unauthorized = NewCode(7, "unauthorized")
IllegalInvoke = NewCode(8, "illegal invoke")
IllegalRequest = NewCode(9, "illegal request")
InvalidPhone = NewCode(10, "invalid phone number")
GoldIsNot = NewCode(11, "invalid Gold number")
IsMaxBetMultiple = NewCode(12, "Max Bet Multiple")
InsufficientDiamond = NewCode(13, "Insufficient diamond")
NotFoundBankCard = NewCode(14, "not found bank card")
ErrorJoinRoom = NewCode(15, "join room error")
ErrorOutCard = NewCode(16, "out card error")
ErrorStrokeCard = NewCode(17, "stroke card error")
ErrorUserFull = NewCode(18, "the room user full ")
ErrorAccount = NewCode(19, "the Mobile is exist !")
ErrorOperationTimeout = NewCode(20, "operation timeout !")
ErrorAgentCode = NewCode(138, "the AgentCode is not exist !")
// 龙虎斗使用
LHDErrorInvalidBet = NewCode(22, "invalid bet")
LHDErrorNotInBetting = NewCode(23, "betting time is over")
LHDErrorBetTooBig = NewCode(24, "betting too big")
LHDErrorPlayerNotInRoom = NewCode(25, "player not in room")
LHDErrorNotEnoughMoney = NewCode(26, "not enough money")
LHDKickPlayer = NewCode(27, "kick player")
LHDErrorBetDragonLimit = NewCode(28, "bet dragon limit")
LHDErrorBetTigerLimit = NewCode(29, "bet tiger limit")
LHDErrorBetDrawLimit = NewCode(30, "bet he limit")
LHDErrorPlayerLimit = NewCode(31, "player limit")
ErrorNeedFirstRecharge = NewCode(32, "player first recharge")
LHDErrorBetTotalLimit = NewCode(33, "bet total limit")
// 拉米使用
RummyNotPickJoker = NewCode(701, "can't pick joker")
)
type Code struct {
code int32
message string
}
func NewCode(code int32, message string) *Code {
return &Code{
code: code,
message: message,
}
}
// Code 返回错误码
func (c *Code) Code() int32 {
return c.code
}
// Message 返回错误码消息
func (c *Code) Message() string {
return c.message
}
// String 格式化错误码
func (c *Code) String() string {
return fmt.Sprintf("code error: code = %d desc = %s", c.code, c.message)
}
// Format 格式化输出
// %s : 打印错误码和错误消息
// %v : 打印错误码、错误消息、错误详情
func (c *Code) Format(s fmt.State, verb rune) {
switch verb {
case 's':
if c.message != "" {
io.WriteString(s, fmt.Sprintf("%d:%s", c.code, c.message))
} else {
io.WriteString(s, fmt.Sprintf("%d", c.code))
}
case 'v':
io.WriteString(s, c.String())
}
}
// Err 转错误消息
func (c *Code) Err() error {
if c.code == OK.Code() {
return nil
}
return &Error{code: c}
}
func (c *Code) ErrWith() {
}
type Error struct {
code *Code
}
func (e *Error) Error() string {
return e.code.String()
}
func Convert(err error) *Code {
if err == nil {
return OK
}
if e, ok := err.(interface{ Code() *Code }); ok {
return e.Code()
}
text := err.Error()
after, found := strings.CutPrefix(text, "code error:")
if !found {
return Unknown
}
after, found = strings.CutPrefix(after, " code = ")
if !found {
return Unknown
}
elements := strings.SplitN(after, " ", 2)
if len(elements) != 2 {
return Unknown
}
code, err := strconv.Atoi(elements[0])
if err != nil {
return Unknown
}
after, found = strings.CutPrefix(elements[1], "desc = ")
if !found {
return Unknown
}
return NewCode(int32(code), after)
}