Merge branch 'errcode' of github.com:OpenIMSDK/Open-IM-Server into errcode

This commit is contained in:
wangchuxiao 2023-01-12 16:46:37 +08:00
commit 21e44518cb
6 changed files with 86 additions and 83 deletions

View File

@ -24,7 +24,7 @@ func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, rpcCl
trace_log.SetCtxInfo(nCtx, logFuncName, nil, "apiReq", apiReq) trace_log.SetCtxInfo(nCtx, logFuncName, nil, "apiReq", apiReq)
etcdConn, err := getcdv3.GetConn(nCtx, rpcName) etcdConn, err := getcdv3.GetConn(nCtx, rpcName)
if err != nil { if err != nil {
trace_log.WriteErrorResponse(nCtx, "GetDefaultConn", err) trace_log.WriteErrorResponse(nCtx, "GetConn", err)
return return
} }
rpc := reflect.ValueOf(rpcClientFunc).Call([]reflect.Value{ rpc := reflect.ValueOf(rpcClientFunc).Call([]reflect.Value{

View File

@ -115,7 +115,18 @@ func (s *groupServer) Run() {
log.NewInfo("", "group rpc success") log.NewInfo("", "group rpc success")
} }
func OperationID(ctx context.Context) string {
s, _ := ctx.Value("operationID").(string)
return s
}
func OpUserID(ctx context.Context) string {
s, _ := ctx.Value("opUserID").(string)
return s
}
func (s *groupServer) CreateGroup(ctx context.Context, req *pbGroup.CreateGroupReq) (*pbGroup.CreateGroupResp, error) { func (s *groupServer) CreateGroup(ctx context.Context, req *pbGroup.CreateGroupReq) (*pbGroup.CreateGroupResp, error) {
resp := &pbGroup.CreateGroupResp{GroupInfo: &open_im_sdk.GroupInfo{}} resp := &pbGroup.CreateGroupResp{GroupInfo: &open_im_sdk.GroupInfo{}}
if err := token_verify.CheckAccessV2(ctx, req.OpUserID, req.OwnerUserID); err != nil { if err := token_verify.CheckAccessV2(ctx, req.OpUserID, req.OwnerUserID); err != nil {
return nil, err return nil, err

View File

@ -16,98 +16,90 @@ type ErrInfo struct {
DetailErrMsg string DetailErrMsg string
} }
func (e ErrInfo) Error() string { func (e *ErrInfo) Error() string {
return e.ErrMsg return e.ErrMsg
} }
func (e ErrInfo) Code() int32 { func (e *ErrInfo) Code() int32 {
return e.ErrCode return e.ErrCode
} }
var ( var (
ErrNone = ErrInfo{0, "", ""} ErrNone = &ErrInfo{0, "", ""}
ErrRpcConn = ErrInfo{GRPCConnIsNil, "grpc conn is nil", ""} ErrArgs = &ErrInfo{ArgsError, "ArgsError", ""}
ErrArgs = ErrInfo{ArgsError, "ArgsError", ""} ErrDatabase = &ErrInfo{DatabaseError, "DatabaseError", ""}
ErrDatabase = ErrInfo{DatabaseError, "DatabaseError", ""} ErrInternalServer = &ErrInfo{ServerInternalError, "ServerInternalError", ""}
ErrInternalServer = ErrInfo{ServerInternalError, "ServerInternalError", ""} ErrNetwork = &ErrInfo{NetworkError, "NetworkError", ""}
ErrNetwork = ErrInfo{NetworkError, "NetworkError", ""} ErrNoPermission = &ErrInfo{NoPermissionError, "NoPermissionError", ""}
ErrNoPermission = ErrInfo{NoPermissionError, "NoPermissionError", ""} ErrIdentity = &ErrInfo{IdentityError, "IdentityError", ""}
ErrIdentity = ErrInfo{IdentityError, "IdentityError", ""} ErrCallback = &ErrInfo{ErrMsg: "CallbackError"}
ErrCallback = ErrInfo{ErrMsg: "CallbackError"} ErrCallbackContinue = &ErrInfo{ErrMsg: "CallbackContinueError"}
ErrCallbackContinue = ErrInfo{ErrMsg: "CallbackContinueError"}
ErrUserIDNotFound = ErrInfo{UserIDNotFoundError, "UserIDNotFoundError", ""} ErrUserIDNotFound = &ErrInfo{UserIDNotFoundError, "UserIDNotFoundError", ""}
ErrGroupIDNotFound = ErrInfo{GroupIDNotFoundError, "GroupIDNotFoundError", ""} ErrGroupIDNotFound = &ErrInfo{GroupIDNotFoundError, "GroupIDNotFoundError", ""}
ErrRecordNotFound = ErrInfo{RecordNotFoundError, "RecordNotFoundError", ""} ErrRecordNotFound = &ErrInfo{RecordNotFoundError, "RecordNotFoundError", ""}
ErrRelationshipAlready = ErrInfo{RelationshipAlreadyError, "RelationshipAlreadyError", ""} ErrRelationshipAlready = &ErrInfo{RelationshipAlreadyError, "RelationshipAlreadyError", ""}
ErrNotRelationshipYet = ErrInfo{NotRelationshipYetError, "NotRelationshipYetError", ""} ErrNotRelationshipYet = &ErrInfo{NotRelationshipYetError, "NotRelationshipYetError", ""}
ErrOnlyOneOwner = ErrInfo{OnlyOneOwnerError, "OnlyOneOwnerError", ""} ErrOnlyOneOwner = &ErrInfo{OnlyOneOwnerError, "OnlyOneOwnerError", ""}
ErrInGroupAlready = ErrInfo{InGroupAlreadyError, "InGroupAlreadyError", ""} ErrInGroupAlready = &ErrInfo{InGroupAlreadyError, "InGroupAlreadyError", ""}
ErrNotInGroupYet = ErrInfo{NotInGroupYetError, "NotInGroupYetError", ""} ErrNotInGroupYet = &ErrInfo{NotInGroupYetError, "NotInGroupYetError", ""}
ErrDismissedAlready = ErrInfo{DismissedAlreadyError, "DismissedAlreadyError", ""} ErrDismissedAlready = &ErrInfo{DismissedAlreadyError, "DismissedAlreadyError", ""}
ErrOwnerNotAllowedQuit = ErrInfo{OwnerNotAllowedQuitError, "OwnerNotAllowedQuitError", ""} ErrOwnerNotAllowedQuit = &ErrInfo{OwnerNotAllowedQuitError, "OwnerNotAllowedQuitError", ""}
ErrRegisteredAlready = ErrInfo{RegisteredAlreadyError, "RegisteredAlreadyError", ""} ErrRegisteredAlready = &ErrInfo{RegisteredAlreadyError, "RegisteredAlreadyError", ""}
ErrGroupTypeNotSupport = ErrInfo{GroupTypeNotSupport, "", ""} ErrGroupTypeNotSupport = &ErrInfo{GroupTypeNotSupport, "", ""}
ErrGroupNoOwner = ErrInfo{GroupNoOwner, "ErrGroupNoOwner", ""} ErrGroupNoOwner = &ErrInfo{GroupNoOwner, "ErrGroupNoOwner", ""}
ErrDefaultOther = ErrInfo{DefaultOtherError, "DefaultOtherError", ""} ErrDefaultOther = &ErrInfo{DefaultOtherError, "DefaultOtherError", ""}
ErrData = ErrInfo{DataError, "DataError", ""} ErrData = &ErrInfo{DataError, "DataError", ""}
ErrTokenExpired = ErrInfo{TokenExpiredError, "TokenExpiredError", ""} ErrTokenExpired = &ErrInfo{TokenExpiredError, "TokenExpiredError", ""}
ErrTokenInvalid = ErrInfo{TokenInvalidError, "TokenInvalidError", ""} // ErrTokenInvalid = &ErrInfo{TokenInvalidError, "TokenInvalidError", ""} //
ErrTokenMalformed = ErrInfo{TokenMalformedError, "TokenMalformedError", ""} //格式错误 ErrTokenMalformed = &ErrInfo{TokenMalformedError, "TokenMalformedError", ""} //格式错误
ErrTokenNotValidYet = ErrInfo{TokenNotValidYetError, "TokenNotValidYetError", ""} //还未生效 ErrTokenNotValidYet = &ErrInfo{TokenNotValidYetError, "TokenNotValidYetError", ""} //还未生效
ErrTokenUnknown = ErrInfo{TokenUnknownError, "TokenUnknownError", ""} //未知错误 ErrTokenUnknown = &ErrInfo{TokenUnknownError, "TokenUnknownError", ""} //未知错误
ErrTokenKicked = ErrInfo{TokenKickedError, "TokenKickedError", ""} ErrTokenKicked = &ErrInfo{TokenKickedError, "TokenKickedError", ""}
ErrTokenNotExist = ErrInfo{TokenNotExistError, "TokenNotExistError", ""} //在redis中不存在 ErrTokenNotExist = &ErrInfo{TokenNotExistError, "TokenNotExistError", ""} //在redis中不存在
ErrTokenDifferentPlatformID = ErrInfo{TokenDifferentPlatformIDError, "TokenDifferentPlatformIDError", ""} ErrTokenDifferentPlatformID = &ErrInfo{TokenDifferentPlatformIDError, "TokenDifferentPlatformIDError", ""}
ErrTokenDifferentUserID = ErrInfo{TokenDifferentUserIDError, "TokenDifferentUserIDError", ""} ErrTokenDifferentUserID = &ErrInfo{TokenDifferentUserIDError, "TokenDifferentUserIDError", ""}
ErrMessageHasReadDisable = ErrInfo{MessageHasReadDisable, "MessageHasReadDisable", ""} ErrMessageHasReadDisable = &ErrInfo{MessageHasReadDisable, "MessageHasReadDisable", ""}
ErrDB = ErrDatabase ErrDB = ErrDatabase
ErrSendLimit = ErrInternalServer ErrSendLimit = ErrInternalServer
) )
func NewErrNetwork(err error) error { func NewErrNetwork(err error) error {
newErrNetwork := ErrNetwork return toDetail(err, ErrNetwork)
newErrNetwork.DetailErrMsg = err.Error()
return ErrNetwork
} }
func NewErrData(err error) error { func NewErrData(err error) error {
newErrData := ErrData return toDetail(err, ErrData)
newErrData.DetailErrMsg = err.Error()
return ErrNetwork
} }
func toDetail(err error, info ErrInfo) ErrInfo { func toDetail(err error, info *ErrInfo) *ErrInfo {
errInfo := info errInfo := *info
errInfo.DetailErrMsg = err.Error() errInfo.DetailErrMsg = err.Error()
return errInfo return &errInfo
} }
func ToAPIErrWithErr(err error) ErrInfo { func ToAPIErrWithErr(err error) *ErrInfo {
errComm := errors.New("")
var marshalErr *json.MarshalerError
errInfo := &ErrInfo{}
switch { switch {
case errors.Is(err, gorm.ErrRecordNotFound): case errors.As(err, &errComm):
if errors.Is(err, gorm.ErrRecordNotFound) {
return toDetail(err, ErrRecordNotFound) return toDetail(err, ErrRecordNotFound)
case errors.Is(err, ErrArgs):
return toDetail(err, ErrArgs)
case errors.Is(err, ErrDatabase):
return ErrDatabase
} }
return toDetail(err, ErrData)
errTarget := errors.New("") case errors.As(err, &marshalErr):
var mErr *json.MarshalerError return toDetail(err, ErrData)
switch { case errors.As(err, &errInfo):
case errors.As(err, &mErr): return toDetail(err, errInfo)
return ErrData
case errors.As(err, errTarget):
return ErrDatabase
} }
return ErrDefaultOther return toDetail(err, ErrDefaultOther)
} }
func SetErrorForResp(err error, commonResp *sdkws.CommonResp) { func SetErrorForResp(err error, commonResp *sdkws.CommonResp) {

View File

@ -31,7 +31,7 @@ func Get(url string) (response []byte, err error) {
return body, nil return body, nil
} }
//application/json; charset=utf-8 // application/json; charset=utf-8
func Post(url string, data interface{}, timeOutSecond int) (content []byte, err error) { func Post(url string, data interface{}, timeOutSecond int) (content []byte, err error) {
jsonStr, err := json.Marshal(data) jsonStr, err := json.Marshal(data)
if err != nil { if err != nil {
@ -65,13 +65,13 @@ func CallBackPostReturn(url, callbackCommand string, input interface{}, output c
b, err := Post(url, input, timeOut) b, err := Post(url, input, timeOut)
if err != nil { if err != nil {
if failedContinue != nil && *failedContinue { if failedContinue != nil && *failedContinue {
return constant.ErrCallbackContinue return &constant.ErrCallbackContinue
} }
return constant.NewErrNetwork(err) return constant.NewErrNetwork(err)
} }
if err = json.Unmarshal(b, output); err != nil { if err = json.Unmarshal(b, output); err != nil {
if failedContinue != nil && *failedContinue { if failedContinue != nil && *failedContinue {
return constant.ErrCallbackContinue return &constant.ErrCallbackContinue
} }
return constant.NewErrData(err) return constant.NewErrData(err)
} }

View File

@ -104,22 +104,22 @@ func GetClaimFromToken(tokensString string) (*Claims, error) {
if err != nil { if err != nil {
if ve, ok := err.(*jwt.ValidationError); ok { if ve, ok := err.(*jwt.ValidationError); ok {
if ve.Errors&jwt.ValidationErrorMalformed != 0 { if ve.Errors&jwt.ValidationErrorMalformed != 0 {
return nil, utils.Wrap(constant.ErrTokenMalformed, "") return nil, utils.Wrap(&constant.ErrTokenMalformed, "")
} else if ve.Errors&jwt.ValidationErrorExpired != 0 { } else if ve.Errors&jwt.ValidationErrorExpired != 0 {
return nil, utils.Wrap(constant.ErrTokenExpired, "") return nil, utils.Wrap(&constant.ErrTokenExpired, "")
} else if ve.Errors&jwt.ValidationErrorNotValidYet != 0 { } else if ve.Errors&jwt.ValidationErrorNotValidYet != 0 {
return nil, utils.Wrap(constant.ErrTokenNotValidYet, "") return nil, utils.Wrap(&constant.ErrTokenNotValidYet, "")
} else { } else {
return nil, utils.Wrap(constant.ErrTokenUnknown, "") return nil, utils.Wrap(&constant.ErrTokenUnknown, "")
} }
} else { } else {
return nil, utils.Wrap(constant.ErrTokenUnknown, "") return nil, utils.Wrap(&constant.ErrTokenUnknown, "")
} }
} else { } else {
if claims, ok := token.Claims.(*Claims); ok && token.Valid { if claims, ok := token.Claims.(*Claims); ok && token.Valid {
return claims, nil return claims, nil
} }
return nil, utils.Wrap(constant.ErrTokenUnknown, "") return nil, utils.Wrap(&constant.ErrTokenUnknown, "")
} }
} }
@ -162,7 +162,7 @@ func CheckAccessV2(ctx context.Context, OpUserID string, OwnerUserID string) (er
if OpUserID == OwnerUserID { if OpUserID == OwnerUserID {
return nil return nil
} }
return utils.Wrap(constant.ErrIdentity, open_utils.GetSelfFuncName()) return utils.Wrap(&constant.ErrIdentity, open_utils.GetSelfFuncName())
} }
func GetUserIDFromToken(token string, operationID string) (bool, string, string) { func GetUserIDFromToken(token string, operationID string) (bool, string, string) {
@ -211,26 +211,26 @@ func ParseToken(tokensString, operationID string) (claims *Claims, err error) {
m, err := commonDB.DB.GetTokenMapByUidPid(claims.UID, claims.Platform) m, err := commonDB.DB.GetTokenMapByUidPid(claims.UID, claims.Platform)
if err != nil { if err != nil {
log.NewError(operationID, "get token from redis err", err.Error(), claims.UID, claims.Platform) log.NewError(operationID, "get token from redis err", err.Error(), claims.UID, claims.Platform)
return nil, utils.Wrap(constant.ErrTokenNotExist, "get token from redis err") return nil, utils.Wrap(&constant.ErrTokenNotExist, "get token from redis err")
} }
if m == nil { if m == nil {
log.NewError(operationID, "get token from redis err, not in redis ", "m is nil ", claims.UID, claims.Platform) log.NewError(operationID, "get token from redis err, not in redis ", "m is nil ", claims.UID, claims.Platform)
return nil, utils.Wrap(constant.ErrTokenNotExist, "get token from redis err") return nil, utils.Wrap(&constant.ErrTokenNotExist, "get token from redis err")
} }
if v, ok := m[tokensString]; ok { if v, ok := m[tokensString]; ok {
switch v { switch v {
case constant.NormalToken: case constant.NormalToken:
log.NewDebug(operationID, "this is normal return", claims) log.NewDebug(operationID, "this is normal return ", *claims)
return claims, nil return claims, nil
case constant.KickedToken: case constant.KickedToken:
log.Error(operationID, "this token has been kicked by other same terminal ", constant.ErrTokenKicked) log.Error(operationID, "this token has been kicked by other same terminal ", constant.ErrTokenKicked)
return nil, utils.Wrap(constant.ErrTokenKicked, "this token has been kicked by other same terminal ") return nil, utils.Wrap(&constant.ErrTokenKicked, "this token has been kicked by other same terminal ")
default: default:
return nil, utils.Wrap(constant.ErrTokenUnknown, "") return nil, utils.Wrap(&constant.ErrTokenUnknown, "")
} }
} }
log.NewError(operationID, "redis token map not find ", constant.ErrTokenNotExist, tokensString) log.NewError(operationID, "redis token map not find ", constant.ErrTokenNotExist, tokensString)
return nil, utils.Wrap(constant.ErrTokenNotExist, "redis token map not find") return nil, utils.Wrap(&constant.ErrTokenNotExist, "redis token map not find")
} }
//func MakeTheTokenInvalid(currentClaims *Claims, platformClass string) (bool, error) { //func MakeTheTokenInvalid(currentClaims *Claims, platformClass string) (bool, error) {
@ -286,11 +286,11 @@ func WsVerifyToken(token, uid string, platformID string, operationID string) (bo
} }
if claims.UID != uid { if claims.UID != uid {
errMsg := " uid is not same to token uid " + argMsg + " claims.UID: " + claims.UID errMsg := " uid is not same to token uid " + argMsg + " claims.UID: " + claims.UID
return false, utils.Wrap(constant.ErrTokenDifferentUserID, errMsg), errMsg return false, utils.Wrap(&constant.ErrTokenDifferentUserID, errMsg), errMsg
} }
if claims.Platform != constant.PlatformIDToName(utils.StringToInt(platformID)) { if claims.Platform != constant.PlatformIDToName(utils.StringToInt(platformID)) {
errMsg := " platform is not same to token platform " + argMsg + " claims platformID: " + claims.Platform errMsg := " platform is not same to token platform " + argMsg + " claims platformID: " + claims.Platform
return false, utils.Wrap(constant.ErrTokenDifferentPlatformID, errMsg), errMsg return false, utils.Wrap(&constant.ErrTokenDifferentPlatformID, errMsg), errMsg
} }
log.NewDebug(operationID, utils.GetSelfFuncName(), " check ok ", claims.UID, uid, claims.Platform) log.NewDebug(operationID, utils.GetSelfFuncName(), " check ok ", claims.UID, uid, claims.Platform)
return true, nil, "" return true, nil, ""

View File

@ -21,7 +21,7 @@ func GetConn(ctx context.Context, serviceName string) (conn *grpc.ClientConn, er
conn = getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), conn = getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","),
serviceName, trace_log.GetOperationID(ctx), config.Config.Etcd.UserName, config.Config.Etcd.Password) serviceName, trace_log.GetOperationID(ctx), config.Config.Etcd.UserName, config.Config.Etcd.Password)
if conn == nil { if conn == nil {
return nil, constant.ErrRpcConn return nil, &constant.ErrInternalServer
} }
return conn, nil return conn, nil
} }