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
|
||||
|
||||
import (
|
||||
common "Open_IM/internal/api_to_rpc"
|
||||
api "Open_IM/pkg/base_info"
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/constant"
|
||||
@ -510,43 +511,8 @@ func GetRecvGroupApplicationList(c *gin.Context) {
|
||||
// @Failure 400 {object} api.Swagger400Resp "errCode为400 一般为参数输入错误, token未带上等"
|
||||
// @Router /group/get_user_req_group_applicationList [post]
|
||||
func GetUserReqGroupApplicationList(c *gin.Context) {
|
||||
var params api.GetUserReqGroupApplicationListReq
|
||||
if err := c.BindJSON(¶ms); err != nil {
|
||||
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)
|
||||
common.ApiToRpc(c, &api.GetUserReqGroupApplicationListReq{}, &api.GetUserRespGroupApplicationResp{},
|
||||
config.Config.RpcRegisterName.OpenImGroupName, rpc.NewGroupClient, "GetGroupApplicationList", token_verify.ParseUserIDFromToken)
|
||||
}
|
||||
|
||||
// @Summary 通过群ID列表获取群信息
|
||||
|
@ -11,11 +11,12 @@ import (
|
||||
promePkg "Open_IM/pkg/common/prometheus"
|
||||
"Open_IM/pkg/common/token_verify"
|
||||
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"
|
||||
pbConversation "Open_IM/pkg/proto/conversation"
|
||||
pbGroup "Open_IM/pkg/proto/group"
|
||||
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
|
||||
pbUser "Open_IM/pkg/proto/user"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
@ -771,40 +772,67 @@ func (s *groupServer) GetGroupMembersInfo(ctx context.Context, req *pbGroup.GetG
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func (s *groupServer) GetGroupApplicationList(_ context.Context, req *pbGroup.GetGroupApplicationListReq) (*pbGroup.GetGroupApplicationListResp, error) {
|
||||
log.NewInfo(req.OperationID, "GetGroupApplicationList args ", req.String())
|
||||
reply, err := imdb.GetGroupApplicationList(req.FromUserID)
|
||||
func FillGroupInfoByGroupID(operationID, groupID string, groupInfo *open_im_sdk.GroupInfo) error {
|
||||
group, err := imdb.TakeGroupInfoByGroupID(groupID)
|
||||
if err != nil {
|
||||
log.NewError(req.OperationID, "GetGroupApplicationList failed ", err.Error(), req.FromUserID)
|
||||
return &pbGroup.GetGroupApplicationListResp{ErrCode: 701, ErrMsg: "GetGroupApplicationList failed"}, nil
|
||||
}
|
||||
|
||||
log.NewDebug(req.OperationID, "GetGroupApplicationList reply ", reply)
|
||||
resp := pbGroup.GetGroupApplicationListResp{}
|
||||
for _, v := range reply {
|
||||
node := open_im_sdk.GroupRequest{UserInfo: &open_im_sdk.PublicUserInfo{}, GroupInfo: &open_im_sdk.GroupInfo{}}
|
||||
group, err := imdb.GetGroupInfoByGroupID(v.GroupID)
|
||||
if err != nil {
|
||||
log.Error(req.OperationID, "GetGroupInfoByGroupID failed ", err.Error(), v.GroupID)
|
||||
continue
|
||||
log.Error(operationID, "TakeGroupInfoByGroupID failed ", err.Error(), groupID)
|
||||
return utils.Wrap(err, "")
|
||||
}
|
||||
if group.Status == constant.GroupStatusDismissed {
|
||||
log.Debug(req.OperationID, "group constant.GroupStatusDismissed ", group.GroupID)
|
||||
continue
|
||||
log.Debug(operationID, " group constant.GroupStatusDismissed ", group.GroupID)
|
||||
return utils.Wrap(constant.ErrGroupStatusDismissed, "")
|
||||
}
|
||||
user, err := imdb.GetUserByUserID(v.UserID)
|
||||
return utils.Wrap(cp.GroupDBCopyOpenIM(groupInfo, group), "")
|
||||
}
|
||||
|
||||
func FillPublicUserInfoByUserID(operationID, userID string, userInfo *open_im_sdk.PublicUserInfo) error {
|
||||
user, err := imdb.TakeUserByUserID(userID)
|
||||
if err != nil {
|
||||
log.Error(req.OperationID, "GetUserByUserID failed ", err.Error(), v.UserID)
|
||||
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{}
|
||||
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 {
|
||||
node := open_im_sdk.GroupRequest{UserInfo: &open_im_sdk.PublicUserInfo{}, GroupInfo: &open_im_sdk.GroupInfo{}}
|
||||
err := FillGroupInfoByGroupID(req.OperationID, v.GroupID, node.GroupInfo)
|
||||
if err != nil {
|
||||
if !errors.Is(errors.Unwrap(err), constant.ErrDismissedAlready) {
|
||||
errResult = err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
err = FillPublicUserInfoByUserID(req.OperationID, v.UserID, node.UserInfo)
|
||||
if err != nil {
|
||||
errResult = err
|
||||
continue
|
||||
}
|
||||
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)
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,10 @@
|
||||
package constant
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ErrInfo struct {
|
||||
ErrCode int32
|
||||
ErrMsg string
|
||||
@ -24,6 +29,8 @@ var (
|
||||
ErrUserIDNotFound = ErrInfo{UserIDNotFoundError, "UserIDNotFoundError"}
|
||||
ErrGroupIDNotFound = ErrInfo{GroupIDNotFoundError, "GroupIDNotFoundError"}
|
||||
|
||||
ErrRecordNotFound = ErrInfo{RecordNotFoundError, "RecordNotFoundError"}
|
||||
|
||||
ErrRelationshipAlready = ErrInfo{RelationshipAlreadyError, "RelationshipAlreadyError"}
|
||||
ErrNotRelationshipYet = ErrInfo{NotRelationshipYetError, "NotRelationshipYetError"}
|
||||
|
||||
@ -47,6 +54,28 @@ var (
|
||||
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 (
|
||||
FormattingError = 10001
|
||||
HasRegistered = 10002
|
||||
@ -80,6 +109,7 @@ const (
|
||||
const (
|
||||
UserIDNotFoundError = 91001 //UserID不存在 或未注册
|
||||
GroupIDNotFoundError = 91002 //GroupID不存在
|
||||
RecordNotFoundError = 91002 //记录不存在
|
||||
)
|
||||
|
||||
// 关系链错误码
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db"
|
||||
"Open_IM/pkg/utils"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
@ -49,7 +48,6 @@ func BatchInsertIntoGroupMember(toInsertInfoList []*db.GroupMember) error {
|
||||
func GetGroupMemberListByUserID(userID string) ([]db.GroupMember, error) {
|
||||
var groupMemberList []db.GroupMember
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
@ -136,8 +134,7 @@ func GetGroupOwnerInfoByGroupID(groupID string) (*db.GroupMember, error) {
|
||||
return &v, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, utils.Wrap(errors.New("no owner"), "")
|
||||
return nil, utils.Wrap(constant.ErrNoGroupOwner, "")
|
||||
}
|
||||
|
||||
func IsExistGroupMember(groupID, userID string) bool {
|
||||
|
@ -38,6 +38,12 @@ func InsertIntoGroup(groupInfo db.Group) error {
|
||||
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) {
|
||||
var groupInfo db.Group
|
||||
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
|
||||
}
|
||||
|
||||
//received
|
||||
func GetGroupApplicationList(userID string) ([]db.GroupRequest, error) {
|
||||
// received
|
||||
func GetRecvGroupApplicationList(userID string) ([]db.GroupRequest, error) {
|
||||
var groupRequestList []db.GroupRequest
|
||||
memberList, err := GetGroupMemberListByUserID(userID)
|
||||
if err != nil {
|
||||
@ -81,12 +81,9 @@ func GetGroupApplicationList(userID string) ([]db.GroupRequest, error) {
|
||||
if v.RoleLevel > constant.GroupOrdinaryUsers {
|
||||
list, err := GetGroupRequestByGroupID(v.GroupID)
|
||||
if err != nil {
|
||||
// fmt.Println("111 GetGroupRequestByGroupID failed ", err.Error())
|
||||
continue
|
||||
}
|
||||
// fmt.Println("222 GetGroupRequestByGroupID ok ", list)
|
||||
groupRequestList = append(groupRequestList, list...)
|
||||
// fmt.Println("333 GetGroupRequestByGroupID ok ", groupRequestList)
|
||||
}
|
||||
}
|
||||
return groupRequestList, nil
|
||||
|
@ -55,6 +55,15 @@ func GetAllUser() ([]db.User, error) {
|
||||
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) {
|
||||
var user db.User
|
||||
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, ""
|
||||
}
|
||||
|
||||
func ParseUserIDFromToken(token string, operationID string) (error, string) {
|
||||
func ParseUserIDFromToken(token string, operationID string) (string, error) {
|
||||
claims, err := ParseToken(token, operationID)
|
||||
if err != nil {
|
||||
log.Error(operationID, "ParseToken failed, ", err.Error(), token)
|
||||
return err, ""
|
||||
return "", err
|
||||
}
|
||||
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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user