mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-09 03:25:59 +08:00
Error code standardization
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user