mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-30 14:32:37 +08:00
Merge branch 'errcode' of github.com:OpenIMSDK/Open-IM-Server into errcode
This commit is contained in:
commit
21e44518cb
@ -24,7 +24,7 @@ func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, rpcCl
|
||||
trace_log.SetCtxInfo(nCtx, logFuncName, nil, "apiReq", apiReq)
|
||||
etcdConn, err := getcdv3.GetConn(nCtx, rpcName)
|
||||
if err != nil {
|
||||
trace_log.WriteErrorResponse(nCtx, "GetDefaultConn", err)
|
||||
trace_log.WriteErrorResponse(nCtx, "GetConn", err)
|
||||
return
|
||||
}
|
||||
rpc := reflect.ValueOf(rpcClientFunc).Call([]reflect.Value{
|
||||
|
@ -115,7 +115,18 @@ func (s *groupServer) Run() {
|
||||
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) {
|
||||
|
||||
resp := &pbGroup.CreateGroupResp{GroupInfo: &open_im_sdk.GroupInfo{}}
|
||||
if err := token_verify.CheckAccessV2(ctx, req.OpUserID, req.OwnerUserID); err != nil {
|
||||
return nil, err
|
||||
|
@ -16,98 +16,90 @@ type ErrInfo struct {
|
||||
DetailErrMsg string
|
||||
}
|
||||
|
||||
func (e ErrInfo) Error() string {
|
||||
func (e *ErrInfo) Error() string {
|
||||
return e.ErrMsg
|
||||
}
|
||||
|
||||
func (e ErrInfo) Code() int32 {
|
||||
func (e *ErrInfo) Code() int32 {
|
||||
return e.ErrCode
|
||||
}
|
||||
|
||||
var (
|
||||
ErrNone = ErrInfo{0, "", ""}
|
||||
ErrRpcConn = ErrInfo{GRPCConnIsNil, "grpc conn is nil", ""}
|
||||
ErrArgs = ErrInfo{ArgsError, "ArgsError", ""}
|
||||
ErrDatabase = ErrInfo{DatabaseError, "DatabaseError", ""}
|
||||
ErrInternalServer = ErrInfo{ServerInternalError, "ServerInternalError", ""}
|
||||
ErrNetwork = ErrInfo{NetworkError, "NetworkError", ""}
|
||||
ErrNoPermission = ErrInfo{NoPermissionError, "NoPermissionError", ""}
|
||||
ErrIdentity = ErrInfo{IdentityError, "IdentityError", ""}
|
||||
ErrCallback = ErrInfo{ErrMsg: "CallbackError"}
|
||||
ErrCallbackContinue = ErrInfo{ErrMsg: "CallbackContinueError"}
|
||||
ErrNone = &ErrInfo{0, "", ""}
|
||||
ErrArgs = &ErrInfo{ArgsError, "ArgsError", ""}
|
||||
ErrDatabase = &ErrInfo{DatabaseError, "DatabaseError", ""}
|
||||
ErrInternalServer = &ErrInfo{ServerInternalError, "ServerInternalError", ""}
|
||||
ErrNetwork = &ErrInfo{NetworkError, "NetworkError", ""}
|
||||
ErrNoPermission = &ErrInfo{NoPermissionError, "NoPermissionError", ""}
|
||||
ErrIdentity = &ErrInfo{IdentityError, "IdentityError", ""}
|
||||
ErrCallback = &ErrInfo{ErrMsg: "CallbackError"}
|
||||
ErrCallbackContinue = &ErrInfo{ErrMsg: "CallbackContinueError"}
|
||||
|
||||
ErrUserIDNotFound = ErrInfo{UserIDNotFoundError, "UserIDNotFoundError", ""}
|
||||
ErrGroupIDNotFound = ErrInfo{GroupIDNotFoundError, "GroupIDNotFoundError", ""}
|
||||
ErrUserIDNotFound = &ErrInfo{UserIDNotFoundError, "UserIDNotFoundError", ""}
|
||||
ErrGroupIDNotFound = &ErrInfo{GroupIDNotFoundError, "GroupIDNotFoundError", ""}
|
||||
|
||||
ErrRecordNotFound = ErrInfo{RecordNotFoundError, "RecordNotFoundError", ""}
|
||||
ErrRecordNotFound = &ErrInfo{RecordNotFoundError, "RecordNotFoundError", ""}
|
||||
|
||||
ErrRelationshipAlready = ErrInfo{RelationshipAlreadyError, "RelationshipAlreadyError", ""}
|
||||
ErrNotRelationshipYet = ErrInfo{NotRelationshipYetError, "NotRelationshipYetError", ""}
|
||||
ErrRelationshipAlready = &ErrInfo{RelationshipAlreadyError, "RelationshipAlreadyError", ""}
|
||||
ErrNotRelationshipYet = &ErrInfo{NotRelationshipYetError, "NotRelationshipYetError", ""}
|
||||
|
||||
ErrOnlyOneOwner = ErrInfo{OnlyOneOwnerError, "OnlyOneOwnerError", ""}
|
||||
ErrInGroupAlready = ErrInfo{InGroupAlreadyError, "InGroupAlreadyError", ""}
|
||||
ErrNotInGroupYet = ErrInfo{NotInGroupYetError, "NotInGroupYetError", ""}
|
||||
ErrDismissedAlready = ErrInfo{DismissedAlreadyError, "DismissedAlreadyError", ""}
|
||||
ErrOwnerNotAllowedQuit = ErrInfo{OwnerNotAllowedQuitError, "OwnerNotAllowedQuitError", ""}
|
||||
ErrRegisteredAlready = ErrInfo{RegisteredAlreadyError, "RegisteredAlreadyError", ""}
|
||||
ErrGroupTypeNotSupport = ErrInfo{GroupTypeNotSupport, "", ""}
|
||||
ErrGroupNoOwner = ErrInfo{GroupNoOwner, "ErrGroupNoOwner", ""}
|
||||
ErrOnlyOneOwner = &ErrInfo{OnlyOneOwnerError, "OnlyOneOwnerError", ""}
|
||||
ErrInGroupAlready = &ErrInfo{InGroupAlreadyError, "InGroupAlreadyError", ""}
|
||||
ErrNotInGroupYet = &ErrInfo{NotInGroupYetError, "NotInGroupYetError", ""}
|
||||
ErrDismissedAlready = &ErrInfo{DismissedAlreadyError, "DismissedAlreadyError", ""}
|
||||
ErrOwnerNotAllowedQuit = &ErrInfo{OwnerNotAllowedQuitError, "OwnerNotAllowedQuitError", ""}
|
||||
ErrRegisteredAlready = &ErrInfo{RegisteredAlreadyError, "RegisteredAlreadyError", ""}
|
||||
ErrGroupTypeNotSupport = &ErrInfo{GroupTypeNotSupport, "", ""}
|
||||
ErrGroupNoOwner = &ErrInfo{GroupNoOwner, "ErrGroupNoOwner", ""}
|
||||
|
||||
ErrDefaultOther = ErrInfo{DefaultOtherError, "DefaultOtherError", ""}
|
||||
ErrData = ErrInfo{DataError, "DataError", ""}
|
||||
ErrTokenExpired = ErrInfo{TokenExpiredError, "TokenExpiredError", ""}
|
||||
ErrTokenInvalid = ErrInfo{TokenInvalidError, "TokenInvalidError", ""} //
|
||||
ErrTokenMalformed = ErrInfo{TokenMalformedError, "TokenMalformedError", ""} //格式错误
|
||||
ErrTokenNotValidYet = ErrInfo{TokenNotValidYetError, "TokenNotValidYetError", ""} //还未生效
|
||||
ErrTokenUnknown = ErrInfo{TokenUnknownError, "TokenUnknownError", ""} //未知错误
|
||||
ErrTokenKicked = ErrInfo{TokenKickedError, "TokenKickedError", ""}
|
||||
ErrTokenNotExist = ErrInfo{TokenNotExistError, "TokenNotExistError", ""} //在redis中不存在
|
||||
ErrTokenDifferentPlatformID = ErrInfo{TokenDifferentPlatformIDError, "TokenDifferentPlatformIDError", ""}
|
||||
ErrTokenDifferentUserID = ErrInfo{TokenDifferentUserIDError, "TokenDifferentUserIDError", ""}
|
||||
ErrDefaultOther = &ErrInfo{DefaultOtherError, "DefaultOtherError", ""}
|
||||
ErrData = &ErrInfo{DataError, "DataError", ""}
|
||||
ErrTokenExpired = &ErrInfo{TokenExpiredError, "TokenExpiredError", ""}
|
||||
ErrTokenInvalid = &ErrInfo{TokenInvalidError, "TokenInvalidError", ""} //
|
||||
ErrTokenMalformed = &ErrInfo{TokenMalformedError, "TokenMalformedError", ""} //格式错误
|
||||
ErrTokenNotValidYet = &ErrInfo{TokenNotValidYetError, "TokenNotValidYetError", ""} //还未生效
|
||||
ErrTokenUnknown = &ErrInfo{TokenUnknownError, "TokenUnknownError", ""} //未知错误
|
||||
ErrTokenKicked = &ErrInfo{TokenKickedError, "TokenKickedError", ""}
|
||||
ErrTokenNotExist = &ErrInfo{TokenNotExistError, "TokenNotExistError", ""} //在redis中不存在
|
||||
ErrTokenDifferentPlatformID = &ErrInfo{TokenDifferentPlatformIDError, "TokenDifferentPlatformIDError", ""}
|
||||
ErrTokenDifferentUserID = &ErrInfo{TokenDifferentUserIDError, "TokenDifferentUserIDError", ""}
|
||||
|
||||
ErrMessageHasReadDisable = ErrInfo{MessageHasReadDisable, "MessageHasReadDisable", ""}
|
||||
ErrMessageHasReadDisable = &ErrInfo{MessageHasReadDisable, "MessageHasReadDisable", ""}
|
||||
|
||||
ErrDB = ErrDatabase
|
||||
ErrSendLimit = ErrInternalServer
|
||||
)
|
||||
|
||||
func NewErrNetwork(err error) error {
|
||||
newErrNetwork := ErrNetwork
|
||||
newErrNetwork.DetailErrMsg = err.Error()
|
||||
return ErrNetwork
|
||||
return toDetail(err, ErrNetwork)
|
||||
}
|
||||
|
||||
func NewErrData(err error) error {
|
||||
newErrData := ErrData
|
||||
newErrData.DetailErrMsg = err.Error()
|
||||
return ErrNetwork
|
||||
return toDetail(err, ErrData)
|
||||
}
|
||||
|
||||
func toDetail(err error, info ErrInfo) ErrInfo {
|
||||
errInfo := info
|
||||
func toDetail(err error, info *ErrInfo) *ErrInfo {
|
||||
errInfo := *info
|
||||
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 {
|
||||
case errors.Is(err, gorm.ErrRecordNotFound):
|
||||
case errors.As(err, &errComm):
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return toDetail(err, ErrRecordNotFound)
|
||||
case errors.Is(err, ErrArgs):
|
||||
return toDetail(err, ErrArgs)
|
||||
case errors.Is(err, ErrDatabase):
|
||||
return ErrDatabase
|
||||
}
|
||||
|
||||
errTarget := errors.New("")
|
||||
var mErr *json.MarshalerError
|
||||
switch {
|
||||
case errors.As(err, &mErr):
|
||||
return ErrData
|
||||
case errors.As(err, errTarget):
|
||||
return ErrDatabase
|
||||
return toDetail(err, ErrData)
|
||||
case errors.As(err, &marshalErr):
|
||||
return toDetail(err, ErrData)
|
||||
case errors.As(err, &errInfo):
|
||||
return toDetail(err, errInfo)
|
||||
}
|
||||
return ErrDefaultOther
|
||||
return toDetail(err, ErrDefaultOther)
|
||||
}
|
||||
|
||||
func SetErrorForResp(err error, commonResp *sdkws.CommonResp) {
|
||||
|
@ -65,13 +65,13 @@ func CallBackPostReturn(url, callbackCommand string, input interface{}, output c
|
||||
b, err := Post(url, input, timeOut)
|
||||
if err != nil {
|
||||
if failedContinue != nil && *failedContinue {
|
||||
return constant.ErrCallbackContinue
|
||||
return &constant.ErrCallbackContinue
|
||||
}
|
||||
return constant.NewErrNetwork(err)
|
||||
}
|
||||
if err = json.Unmarshal(b, output); err != nil {
|
||||
if failedContinue != nil && *failedContinue {
|
||||
return constant.ErrCallbackContinue
|
||||
return &constant.ErrCallbackContinue
|
||||
}
|
||||
return constant.NewErrData(err)
|
||||
}
|
||||
|
@ -104,22 +104,22 @@ func GetClaimFromToken(tokensString string) (*Claims, error) {
|
||||
if err != nil {
|
||||
if ve, ok := err.(*jwt.ValidationError); ok {
|
||||
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 {
|
||||
return nil, utils.Wrap(constant.ErrTokenExpired, "")
|
||||
return nil, utils.Wrap(&constant.ErrTokenExpired, "")
|
||||
} else if ve.Errors&jwt.ValidationErrorNotValidYet != 0 {
|
||||
return nil, utils.Wrap(constant.ErrTokenNotValidYet, "")
|
||||
return nil, utils.Wrap(&constant.ErrTokenNotValidYet, "")
|
||||
} else {
|
||||
return nil, utils.Wrap(constant.ErrTokenUnknown, "")
|
||||
return nil, utils.Wrap(&constant.ErrTokenUnknown, "")
|
||||
}
|
||||
} else {
|
||||
return nil, utils.Wrap(constant.ErrTokenUnknown, "")
|
||||
return nil, utils.Wrap(&constant.ErrTokenUnknown, "")
|
||||
}
|
||||
} else {
|
||||
if claims, ok := token.Claims.(*Claims); ok && token.Valid {
|
||||
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 {
|
||||
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) {
|
||||
@ -211,26 +211,26 @@ func ParseToken(tokensString, operationID string) (claims *Claims, err error) {
|
||||
m, err := commonDB.DB.GetTokenMapByUidPid(claims.UID, claims.Platform)
|
||||
if err != nil {
|
||||
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 {
|
||||
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 {
|
||||
switch v {
|
||||
case constant.NormalToken:
|
||||
log.NewDebug(operationID, "this is normal return", claims)
|
||||
log.NewDebug(operationID, "this is normal return ", *claims)
|
||||
return claims, nil
|
||||
case constant.KickedToken:
|
||||
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:
|
||||
return nil, utils.Wrap(constant.ErrTokenUnknown, "")
|
||||
return nil, utils.Wrap(&constant.ErrTokenUnknown, "")
|
||||
}
|
||||
}
|
||||
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) {
|
||||
@ -286,11 +286,11 @@ func WsVerifyToken(token, uid string, platformID string, operationID string) (bo
|
||||
}
|
||||
if claims.UID != 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)) {
|
||||
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)
|
||||
return true, nil, ""
|
||||
|
@ -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, ","),
|
||||
serviceName, trace_log.GetOperationID(ctx), config.Config.Etcd.UserName, config.Config.Etcd.Password)
|
||||
if conn == nil {
|
||||
return nil, constant.ErrRpcConn
|
||||
return nil, &constant.ErrInternalServer
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user