mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-06-18 00:46:41 +08:00
Error code standardization
This commit is contained in:
parent
b2ce96eb67
commit
89a4fe96b5
@ -1,6 +1,7 @@
|
|||||||
package group
|
package group
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
common "Open_IM/internal/api_to_rpc"
|
||||||
api "Open_IM/pkg/base_info"
|
api "Open_IM/pkg/base_info"
|
||||||
"Open_IM/pkg/common/config"
|
"Open_IM/pkg/common/config"
|
||||||
"Open_IM/pkg/common/constant"
|
"Open_IM/pkg/common/constant"
|
||||||
@ -510,43 +511,8 @@ func GetRecvGroupApplicationList(c *gin.Context) {
|
|||||||
// @Failure 400 {object} api.Swagger400Resp "errCode为400 一般为参数输入错误, token未带上等"
|
// @Failure 400 {object} api.Swagger400Resp "errCode为400 一般为参数输入错误, token未带上等"
|
||||||
// @Router /group/get_user_req_group_applicationList [post]
|
// @Router /group/get_user_req_group_applicationList [post]
|
||||||
func GetUserReqGroupApplicationList(c *gin.Context) {
|
func GetUserReqGroupApplicationList(c *gin.Context) {
|
||||||
var params api.GetUserReqGroupApplicationListReq
|
common.ApiToRpc(c, &api.GetUserReqGroupApplicationListReq{}, &api.GetUserRespGroupApplicationResp{},
|
||||||
if err := c.BindJSON(¶ms); err != nil {
|
config.Config.RpcRegisterName.OpenImGroupName, rpc.NewGroupClient, "GetGroupApplicationList", token_verify.ParseUserIDFromToken)
|
||||||
log.NewError("0", utils.GetSelfFuncName(), err.Error())
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
req := &rpc.GetUserReqApplicationListReq{}
|
|
||||||
utils.CopyStructFields(req, params)
|
|
||||||
var ok bool
|
|
||||||
var errInfo string
|
|
||||||
ok, req.OpUserID, errInfo = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), req.OperationID)
|
|
||||||
if !ok {
|
|
||||||
errMsg := req.OperationID + " " + "GetUserIDFromToken failed " + errInfo + " token:" + c.Request.Header.Get("token")
|
|
||||||
log.NewError(req.OperationID, errMsg)
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": errMsg})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.NewInfo(req.OperationID, "GetGroupsInfo args ", req.String())
|
|
||||||
etcdConn := getcdv3.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName, req.OperationID)
|
|
||||||
if etcdConn == nil {
|
|
||||||
errMsg := req.OperationID + "getcdv3.GetDefaultConn == nil"
|
|
||||||
log.NewError(req.OperationID, errMsg)
|
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
client := rpc.NewGroupClient(etcdConn)
|
|
||||||
RpcResp, err := client.GetUserReqApplicationList(context.Background(), req)
|
|
||||||
if err != nil {
|
|
||||||
log.NewError(req.OperationID, "GetGroupsInfo failed ", err.Error(), req.String())
|
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.NewInfo(req.OperationID, RpcResp)
|
|
||||||
resp := api.GetGroupApplicationListResp{CommResp: api.CommResp{ErrCode: RpcResp.CommonResp.ErrCode, ErrMsg: RpcResp.CommonResp.ErrMsg}, GroupRequestList: RpcResp.GroupRequestList}
|
|
||||||
resp.Data = jsonData.JsonDataList(resp.GroupRequestList)
|
|
||||||
log.NewInfo(req.OperationID, "GetGroupApplicationList api return ", resp)
|
|
||||||
c.JSON(http.StatusOK, resp)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Summary 通过群ID列表获取群信息
|
// @Summary 通过群ID列表获取群信息
|
||||||
|
@ -11,11 +11,12 @@ import (
|
|||||||
promePkg "Open_IM/pkg/common/prometheus"
|
promePkg "Open_IM/pkg/common/prometheus"
|
||||||
"Open_IM/pkg/common/token_verify"
|
"Open_IM/pkg/common/token_verify"
|
||||||
cp "Open_IM/pkg/common/utils"
|
cp "Open_IM/pkg/common/utils"
|
||||||
"Open_IM/pkg/grpc-etcdv3/getcdv3"
|
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
|
||||||
|
"github.com/OpenIMSDK/getcdv3"
|
||||||
|
|
||||||
pbCache "Open_IM/pkg/proto/cache"
|
pbCache "Open_IM/pkg/proto/cache"
|
||||||
pbConversation "Open_IM/pkg/proto/conversation"
|
pbConversation "Open_IM/pkg/proto/conversation"
|
||||||
pbGroup "Open_IM/pkg/proto/group"
|
pbGroup "Open_IM/pkg/proto/group"
|
||||||
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
|
|
||||||
pbUser "Open_IM/pkg/proto/user"
|
pbUser "Open_IM/pkg/proto/user"
|
||||||
"Open_IM/pkg/utils"
|
"Open_IM/pkg/utils"
|
||||||
"context"
|
"context"
|
||||||
@ -771,40 +772,67 @@ func (s *groupServer) GetGroupMembersInfo(ctx context.Context, req *pbGroup.GetG
|
|||||||
return &resp, nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *groupServer) GetGroupApplicationList(_ context.Context, req *pbGroup.GetGroupApplicationListReq) (*pbGroup.GetGroupApplicationListResp, error) {
|
func FillGroupInfoByGroupID(operationID, groupID string, groupInfo *open_im_sdk.GroupInfo) error {
|
||||||
log.NewInfo(req.OperationID, "GetGroupApplicationList args ", req.String())
|
group, err := imdb.TakeGroupInfoByGroupID(groupID)
|
||||||
reply, err := imdb.GetGroupApplicationList(req.FromUserID)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.NewError(req.OperationID, "GetGroupApplicationList failed ", err.Error(), req.FromUserID)
|
log.Error(operationID, "TakeGroupInfoByGroupID failed ", err.Error(), groupID)
|
||||||
return &pbGroup.GetGroupApplicationListResp{ErrCode: 701, ErrMsg: "GetGroupApplicationList failed"}, nil
|
return utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
|
if group.Status == constant.GroupStatusDismissed {
|
||||||
|
log.Debug(operationID, " group constant.GroupStatusDismissed ", group.GroupID)
|
||||||
|
return utils.Wrap(constant.ErrGroupStatusDismissed, "")
|
||||||
|
}
|
||||||
|
return utils.Wrap(cp.GroupDBCopyOpenIM(groupInfo, group), "")
|
||||||
|
}
|
||||||
|
|
||||||
log.NewDebug(req.OperationID, "GetGroupApplicationList reply ", reply)
|
func FillPublicUserInfoByUserID(operationID, userID string, userInfo *open_im_sdk.PublicUserInfo) error {
|
||||||
|
user, err := imdb.TakeUserByUserID(userID)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(operationID, "TakeUserByUserID failed ", err.Error(), userID)
|
||||||
|
return utils.Wrap(err, "")
|
||||||
|
}
|
||||||
|
cp.UserDBCopyOpenIMPublicUser(userInfo, user)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *groupServer) GetGroupApplicationList(_ context.Context, req *pbGroup.GetGroupApplicationListReq) (*pbGroup.GetGroupApplicationListResp, error) {
|
||||||
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String())
|
||||||
resp := pbGroup.GetGroupApplicationListResp{}
|
resp := pbGroup.GetGroupApplicationListResp{}
|
||||||
|
reply, err := imdb.GetRecvGroupApplicationList(req.FromUserID)
|
||||||
|
if err != nil {
|
||||||
|
log.NewError(req.OperationID, "GetRecvGroupApplicationList failed ", err.Error(), req.FromUserID)
|
||||||
|
errInfo := constant.ToAPIErrWithErr(err)
|
||||||
|
resp.ErrCode = errInfo.ErrCode
|
||||||
|
resp.ErrMsg = errInfo.ErrMsg
|
||||||
|
return &resp, nil
|
||||||
|
}
|
||||||
|
var errResult error
|
||||||
|
log.NewDebug(req.OperationID, "GetGroupApplicationList from db ", reply)
|
||||||
|
|
||||||
for _, v := range reply {
|
for _, v := range reply {
|
||||||
node := open_im_sdk.GroupRequest{UserInfo: &open_im_sdk.PublicUserInfo{}, GroupInfo: &open_im_sdk.GroupInfo{}}
|
node := open_im_sdk.GroupRequest{UserInfo: &open_im_sdk.PublicUserInfo{}, GroupInfo: &open_im_sdk.GroupInfo{}}
|
||||||
group, err := imdb.GetGroupInfoByGroupID(v.GroupID)
|
err := FillGroupInfoByGroupID(req.OperationID, v.GroupID, node.GroupInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(req.OperationID, "GetGroupInfoByGroupID failed ", err.Error(), v.GroupID)
|
if !errors.Is(errors.Unwrap(err), constant.ErrDismissedAlready) {
|
||||||
continue
|
errResult = err
|
||||||
}
|
}
|
||||||
if group.Status == constant.GroupStatusDismissed {
|
|
||||||
log.Debug(req.OperationID, "group constant.GroupStatusDismissed ", group.GroupID)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
user, err := imdb.GetUserByUserID(v.UserID)
|
|
||||||
if err != nil {
|
|
||||||
log.Error(req.OperationID, "GetUserByUserID failed ", err.Error(), v.UserID)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = FillPublicUserInfoByUserID(req.OperationID, v.UserID, node.UserInfo)
|
||||||
|
if err != nil {
|
||||||
|
errResult = err
|
||||||
|
continue
|
||||||
|
}
|
||||||
cp.GroupRequestDBCopyOpenIM(&node, &v)
|
cp.GroupRequestDBCopyOpenIM(&node, &v)
|
||||||
cp.UserDBCopyOpenIMPublicUser(node.UserInfo, user)
|
|
||||||
cp.GroupDBCopyOpenIM(node.GroupInfo, group)
|
|
||||||
log.NewDebug(req.OperationID, "node ", node, "v ", v)
|
|
||||||
resp.GroupRequestList = append(resp.GroupRequestList, &node)
|
resp.GroupRequestList = append(resp.GroupRequestList, &node)
|
||||||
}
|
}
|
||||||
log.NewInfo(req.OperationID, "GetGroupMembersInfo rpc return ", resp)
|
if errResult != nil && len(resp.GroupRequestList) == 0 {
|
||||||
|
errInfo := constant.ToAPIErrWithErr(errResult)
|
||||||
|
resp.ErrCode = errInfo.ErrCode
|
||||||
|
resp.ErrMsg = errInfo.ErrMsg
|
||||||
|
}
|
||||||
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", resp.String())
|
||||||
return &resp, nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
package constant
|
package constant
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
type ErrInfo struct {
|
type ErrInfo struct {
|
||||||
ErrCode int32
|
ErrCode int32
|
||||||
ErrMsg string
|
ErrMsg string
|
||||||
@ -24,6 +29,8 @@ var (
|
|||||||
ErrUserIDNotFound = ErrInfo{UserIDNotFoundError, "UserIDNotFoundError"}
|
ErrUserIDNotFound = ErrInfo{UserIDNotFoundError, "UserIDNotFoundError"}
|
||||||
ErrGroupIDNotFound = ErrInfo{GroupIDNotFoundError, "GroupIDNotFoundError"}
|
ErrGroupIDNotFound = ErrInfo{GroupIDNotFoundError, "GroupIDNotFoundError"}
|
||||||
|
|
||||||
|
ErrRecordNotFound = ErrInfo{RecordNotFoundError, "RecordNotFoundError"}
|
||||||
|
|
||||||
ErrRelationshipAlready = ErrInfo{RelationshipAlreadyError, "RelationshipAlreadyError"}
|
ErrRelationshipAlready = ErrInfo{RelationshipAlreadyError, "RelationshipAlreadyError"}
|
||||||
ErrNotRelationshipYet = ErrInfo{NotRelationshipYetError, "NotRelationshipYetError"}
|
ErrNotRelationshipYet = ErrInfo{NotRelationshipYetError, "NotRelationshipYetError"}
|
||||||
|
|
||||||
@ -47,6 +54,28 @@ var (
|
|||||||
ErrTokenDifferentUserID = ErrInfo{TokenDifferentUserIDError, "TokenDifferentUserIDError"}
|
ErrTokenDifferentUserID = ErrInfo{TokenDifferentUserIDError, "TokenDifferentUserIDError"}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//var (
|
||||||
|
// ErrGroupStatusDismissed = errors.New("group dismissed")
|
||||||
|
// ErrNoGroupOwner = errors.New("no group owner")
|
||||||
|
//)
|
||||||
|
|
||||||
|
func ToAPIErrWithErr(err error) ErrInfo {
|
||||||
|
errTarget := errors.New("")
|
||||||
|
var errInfo ErrInfo
|
||||||
|
switch {
|
||||||
|
case errors.As(err, &errTarget):
|
||||||
|
if errors.Is(errTarget, gorm.ErrRecordNotFound) {
|
||||||
|
return ErrRecordNotFound
|
||||||
|
}
|
||||||
|
case errors.As(err, &errInfo):
|
||||||
|
if errors.Is(errInfo, ErrArgs) {
|
||||||
|
return ErrArgs
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return ErrDefaultOther
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
FormattingError = 10001
|
FormattingError = 10001
|
||||||
HasRegistered = 10002
|
HasRegistered = 10002
|
||||||
@ -80,6 +109,7 @@ const (
|
|||||||
const (
|
const (
|
||||||
UserIDNotFoundError = 91001 //UserID不存在 或未注册
|
UserIDNotFoundError = 91001 //UserID不存在 或未注册
|
||||||
GroupIDNotFoundError = 91002 //GroupID不存在
|
GroupIDNotFoundError = 91002 //GroupID不存在
|
||||||
|
RecordNotFoundError = 91002 //记录不存在
|
||||||
)
|
)
|
||||||
|
|
||||||
// 关系链错误码
|
// 关系链错误码
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"Open_IM/pkg/common/constant"
|
"Open_IM/pkg/common/constant"
|
||||||
"Open_IM/pkg/common/db"
|
"Open_IM/pkg/common/db"
|
||||||
"Open_IM/pkg/utils"
|
"Open_IM/pkg/utils"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -49,7 +48,6 @@ func BatchInsertIntoGroupMember(toInsertInfoList []*db.GroupMember) error {
|
|||||||
func GetGroupMemberListByUserID(userID string) ([]db.GroupMember, error) {
|
func GetGroupMemberListByUserID(userID string) ([]db.GroupMember, error) {
|
||||||
var groupMemberList []db.GroupMember
|
var groupMemberList []db.GroupMember
|
||||||
err := db.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("user_id=?", userID).Find(&groupMemberList).Error
|
err := db.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("user_id=?", userID).Find(&groupMemberList).Error
|
||||||
//err = dbConn.Table("group_members").Where("user_id=?", userID).Take(&groupMemberList).Error
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -136,8 +134,7 @@ func GetGroupOwnerInfoByGroupID(groupID string) (*db.GroupMember, error) {
|
|||||||
return &v, nil
|
return &v, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nil, utils.Wrap(constant.ErrNoGroupOwner, "")
|
||||||
return nil, utils.Wrap(errors.New("no owner"), "")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsExistGroupMember(groupID, userID string) bool {
|
func IsExistGroupMember(groupID, userID string) bool {
|
||||||
|
@ -38,6 +38,12 @@ func InsertIntoGroup(groupInfo db.Group) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TakeGroupInfoByGroupID(groupID string) (*db.Group, error) {
|
||||||
|
var groupInfo db.Group
|
||||||
|
err := db.DB.MysqlDB.DefaultGormDB().Table("groups").Where("group_id=?", groupID).Take(&groupInfo).Error
|
||||||
|
return &groupInfo, err
|
||||||
|
}
|
||||||
|
|
||||||
func GetGroupInfoByGroupID(groupID string) (*db.Group, error) {
|
func GetGroupInfoByGroupID(groupID string) (*db.Group, error) {
|
||||||
var groupInfo db.Group
|
var groupInfo db.Group
|
||||||
err := db.DB.MysqlDB.DefaultGormDB().Table("groups").Where("group_id=?", groupID).Take(&groupInfo).Error
|
err := db.DB.MysqlDB.DefaultGormDB().Table("groups").Where("group_id=?", groupID).Take(&groupInfo).Error
|
||||||
|
@ -70,8 +70,8 @@ func GetGroupRequestByGroupID(groupID string) ([]db.GroupRequest, error) {
|
|||||||
return groupRequestList, nil
|
return groupRequestList, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//received
|
// received
|
||||||
func GetGroupApplicationList(userID string) ([]db.GroupRequest, error) {
|
func GetRecvGroupApplicationList(userID string) ([]db.GroupRequest, error) {
|
||||||
var groupRequestList []db.GroupRequest
|
var groupRequestList []db.GroupRequest
|
||||||
memberList, err := GetGroupMemberListByUserID(userID)
|
memberList, err := GetGroupMemberListByUserID(userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -81,12 +81,9 @@ func GetGroupApplicationList(userID string) ([]db.GroupRequest, error) {
|
|||||||
if v.RoleLevel > constant.GroupOrdinaryUsers {
|
if v.RoleLevel > constant.GroupOrdinaryUsers {
|
||||||
list, err := GetGroupRequestByGroupID(v.GroupID)
|
list, err := GetGroupRequestByGroupID(v.GroupID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// fmt.Println("111 GetGroupRequestByGroupID failed ", err.Error())
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// fmt.Println("222 GetGroupRequestByGroupID ok ", list)
|
|
||||||
groupRequestList = append(groupRequestList, list...)
|
groupRequestList = append(groupRequestList, list...)
|
||||||
// fmt.Println("333 GetGroupRequestByGroupID ok ", groupRequestList)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return groupRequestList, nil
|
return groupRequestList, nil
|
||||||
|
@ -55,6 +55,15 @@ func GetAllUser() ([]db.User, error) {
|
|||||||
return userList, err
|
return userList, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TakeUserByUserID(userID string) (*db.User, error) {
|
||||||
|
var user db.User
|
||||||
|
err := db.DB.MysqlDB.DefaultGormDB().Table("users").Where("user_id=?", userID).Take(&user).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &user, nil
|
||||||
|
}
|
||||||
|
|
||||||
func GetUserByUserID(userID string) (*db.User, error) {
|
func GetUserByUserID(userID string) (*db.User, error) {
|
||||||
var user db.User
|
var user db.User
|
||||||
err := db.DB.MysqlDB.DefaultGormDB().Table("users").Where("user_id=?", userID).Take(&user).Error
|
err := db.DB.MysqlDB.DefaultGormDB().Table("users").Where("user_id=?", userID).Take(&user).Error
|
||||||
|
@ -159,14 +159,14 @@ func GetUserIDFromToken(token string, operationID string) (bool, string, string)
|
|||||||
return true, claims.UID, ""
|
return true, claims.UID, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseUserIDFromToken(token string, operationID string) (error, string) {
|
func ParseUserIDFromToken(token string, operationID string) (string, error) {
|
||||||
claims, err := ParseToken(token, operationID)
|
claims, err := ParseToken(token, operationID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(operationID, "ParseToken failed, ", err.Error(), token)
|
log.Error(operationID, "ParseToken failed, ", err.Error(), token)
|
||||||
return err, ""
|
return "", err
|
||||||
}
|
}
|
||||||
log.Debug(operationID, "token claims.ExpiresAt.Second() ", claims.ExpiresAt.Unix())
|
log.Debug(operationID, "token claims.ExpiresAt.Second() ", claims.ExpiresAt.Unix())
|
||||||
return nil, claims.UID
|
return claims.UID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUserIDFromTokenExpireTime(token string, operationID string) (bool, string, string, int64) {
|
func GetUserIDFromTokenExpireTime(token string, operationID string) (bool, string, string, int64) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user