Merge remote-tracking branch 'origin/errcode' into errcode

test-errcode
withchao 3 years ago
commit 58206f983e

@ -162,7 +162,7 @@ log:
#日志级别 6表示全都打印测试阶段建议设置为6 #日志级别 6表示全都打印测试阶段建议设置为6
remainLogLevel: -1 remainLogLevel: -1
stderr: true stderr: true
withStack: false
elasticSearchSwitch: false elasticSearchSwitch: false
elasticSearchAddr: [ 127.0.0.1:9201 ] elasticSearchAddr: [ 127.0.0.1:9201 ]
elasticSearchUser: "" elasticSearchUser: ""

@ -2,6 +2,7 @@ package api
import ( import (
"context" "context"
"github.com/OpenIMSDK/Open-IM-Server/pkg/a2r" "github.com/OpenIMSDK/Open-IM-Server/pkg/a2r"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"

@ -2,6 +2,7 @@ package friend
import ( import (
"context" "context"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/relation" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/relation"
@ -177,7 +178,8 @@ func (s *friendServer) GetPaginationFriendsApplyTo(ctx context.Context, req *pbf
if err := s.userCheck.Access(ctx, req.UserID); err != nil { if err := s.userCheck.Access(ctx, req.UserID); err != nil {
return nil, err return nil, err
} }
friendRequests, total, err := s.FriendDatabase.PageFriendRequestToMe(ctx, req.UserID, req.Pagination.PageNumber, req.Pagination.ShowNumber) pageNumber, showNumber := utils.GetPage(req.Pagination)
friendRequests, total, err := s.FriendDatabase.PageFriendRequestToMe(ctx, req.UserID, pageNumber, showNumber)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -195,7 +197,8 @@ func (s *friendServer) GetPaginationFriendsApplyFrom(ctx context.Context, req *p
if err := s.userCheck.Access(ctx, req.UserID); err != nil { if err := s.userCheck.Access(ctx, req.UserID); err != nil {
return nil, err return nil, err
} }
friendRequests, total, err := s.FriendDatabase.PageFriendRequestFromMe(ctx, req.UserID, req.Pagination.PageNumber, req.Pagination.ShowNumber) pageNumber, showNumber := utils.GetPage(req.Pagination)
friendRequests, total, err := s.FriendDatabase.PageFriendRequestFromMe(ctx, req.UserID, pageNumber, showNumber)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -222,7 +225,8 @@ func (s *friendServer) GetPaginationFriends(ctx context.Context, req *pbfriend.G
if err := s.userCheck.Access(ctx, req.UserID); err != nil { if err := s.userCheck.Access(ctx, req.UserID); err != nil {
return nil, err return nil, err
} }
friends, total, err := s.FriendDatabase.PageOwnerFriends(ctx, req.UserID, req.Pagination.PageNumber, req.Pagination.ShowNumber) pageNumber, showNumber := utils.GetPage(req.Pagination)
friends, total, err := s.FriendDatabase.PageOwnerFriends(ctx, req.UserID, pageNumber, showNumber)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -187,6 +187,7 @@ type config struct {
RemainRotationCount uint `yaml:"remainRotationCount"` RemainRotationCount uint `yaml:"remainRotationCount"`
RemainLogLevel int `yaml:"remainLogLevel"` RemainLogLevel int `yaml:"remainLogLevel"`
Stderr bool `yaml:"stderr"` Stderr bool `yaml:"stderr"`
WithStack bool `yaml:"withStack"`
ElasticSearchSwitch bool `yaml:"elasticSearchSwitch"` ElasticSearchSwitch bool `yaml:"elasticSearchSwitch"`
ElasticSearchAddr []string `yaml:"elasticSearchAddr"` ElasticSearchAddr []string `yaml:"elasticSearchAddr"`
ElasticSearchUser string `yaml:"elasticSearchUser"` ElasticSearchUser string `yaml:"elasticSearchUser"`

@ -2,7 +2,7 @@ package controller
import ( import (
"context" "context"
"errors" "time"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
@ -73,7 +73,7 @@ func (f *friendDatabase) AddFriendRequest(ctx context.Context, fromUserID, toUse
return f.tx.Transaction(func(tx any) error { return f.tx.Transaction(func(tx any) error {
_, err := f.friendRequest.NewTx(tx).Take(ctx, fromUserID, toUserID) _, err := f.friendRequest.NewTx(tx).Take(ctx, fromUserID, toUserID)
//有db错误 //有db错误
if err != nil && errors.Unwrap(err) != gorm.ErrRecordNotFound { if err != nil && errs.Unwrap(err) != gorm.ErrRecordNotFound {
return err return err
} }
//无错误 则更新 //无错误 则更新
@ -89,7 +89,7 @@ func (f *friendDatabase) AddFriendRequest(ctx context.Context, fromUserID, toUse
return nil return nil
} }
//gorm.ErrRecordNotFound 错误,则新增 //gorm.ErrRecordNotFound 错误,则新增
if err := f.friendRequest.NewTx(tx).Create(ctx, []*relation.FriendRequestModel{{FromUserID: fromUserID, ToUserID: toUserID, ReqMsg: reqMsg, Ex: ex}}); err != nil { if err := f.friendRequest.NewTx(tx).Create(ctx, []*relation.FriendRequestModel{{FromUserID: fromUserID, ToUserID: toUserID, ReqMsg: reqMsg, Ex: ex, CreateTime: time.Now(), HandleTime: time.Unix(0, 0)}}); err != nil {
return err return err
} }
return nil return nil
@ -142,7 +142,8 @@ func (f *friendDatabase) RefuseFriendRequest(ctx context.Context, friendRequest
return err return err
} }
friendRequest.HandleResult = constant.FriendResponseRefuse friendRequest.HandleResult = constant.FriendResponseRefuse
err = f.friendRequest.Update(ctx, []*relation.FriendRequestModel{friendRequest}) friendRequest.HandleTime = time.Now()
err = f.friendRequest.Update(ctx, friendRequest)
if err != nil { if err != nil {
return err return err
} }
@ -158,7 +159,8 @@ func (f *friendDatabase) AgreeFriendRequest(ctx context.Context, friendRequest *
} }
friendRequest.HandlerUserID = friendRequest.FromUserID friendRequest.HandlerUserID = friendRequest.FromUserID
friendRequest.HandleResult = constant.FriendResponseAgree friendRequest.HandleResult = constant.FriendResponseAgree
err = f.friendRequest.NewTx(tx).Update(ctx, []*relation.FriendRequestModel{friendRequest}) friendRequest.HandleTime = time.Now()
err = f.friendRequest.NewTx(tx).Update(ctx, friendRequest)
if err != nil { if err != nil {
return err return err
} }

@ -31,13 +31,13 @@ func (f *FriendRequestGorm) Delete(ctx context.Context, fromUserID, toUserID str
} }
// 更新零值 // 更新零值
func (f *FriendRequestGorm) UpdateByMap(ctx context.Context, formUserID string, toUserID string, args map[string]interface{}) (err error) { func (f *FriendRequestGorm) UpdateByMap(ctx context.Context, fromUserID string, toUserID string, args map[string]interface{}) (err error) {
return utils.Wrap(f.db(ctx).Model(&relation.FriendRequestModel{}).Where("from_user_id = ? AND to_user_id =?", formUserID, toUserID).Updates(args).Error, "") return utils.Wrap(f.db(ctx).Model(&relation.FriendRequestModel{}).Where("from_user_id = ? AND to_user_id =?", fromUserID, toUserID).Updates(args).Error, "")
} }
// 更新多条记录 (非零值) // 更新记录 (非零值)
func (f *FriendRequestGorm) Update(ctx context.Context, friendRequests []*relation.FriendRequestModel) (err error) { func (f *FriendRequestGorm) Update(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error) {
return utils.Wrap(f.db(ctx).Updates(&friendRequests).Error, "") return utils.Wrap(f.db(ctx).Where("from_user_id = ? AND to_user_id =?", friendRequest.FromUserID, friendRequest.ToUserID).Updates(friendRequest).Error, "")
} }
// 获取来指定用户的好友申请 未找到 不返回错误 // 获取来指定用户的好友申请 未找到 不返回错误

@ -31,7 +31,7 @@ type FriendRequestModelInterface interface {
// 更新零值 // 更新零值
UpdateByMap(ctx context.Context, formUserID string, toUserID string, args map[string]interface{}) (err error) UpdateByMap(ctx context.Context, formUserID string, toUserID string, args map[string]interface{}) (err error)
// 更新多条记录 (非零值) // 更新多条记录 (非零值)
Update(ctx context.Context, friendRequests []*FriendRequestModel) (err error) Update(ctx context.Context, friendRequest *FriendRequestModel) (err error)
// 获取来指定用户的好友申请 未找到 不返回错误 // 获取来指定用户的好友申请 未找到 不返回错误
Find(ctx context.Context, fromUserID, toUserID string) (friendRequest *FriendRequestModel, err error) Find(ctx context.Context, fromUserID, toUserID string) (friendRequest *FriendRequestModel, err error)
Take(ctx context.Context, fromUserID, toUserID string) (friendRequest *FriendRequestModel, err error) Take(ctx context.Context, fromUserID, toUserID string) (friendRequest *FriendRequestModel, err error)

@ -3,6 +3,12 @@ package mw
import ( import (
"context" "context"
"fmt" "fmt"
"math"
"runtime"
"runtime/debug"
"strings"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mw/specialerror" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/mw/specialerror"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs" "github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
@ -12,10 +18,6 @@ import (
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
"math"
"runtime"
"runtime/debug"
"strings"
) )
const OperationID = "operationID" const OperationID = "operationID"
@ -87,23 +89,25 @@ func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary
} }
grpcStatus := status.New(codes.Code(code), codeErr.Msg()) grpcStatus := status.New(codes.Code(code), codeErr.Msg())
var errInfo *errinfo.ErrorInfo var errInfo *errinfo.ErrorInfo
if unwrap != err { if config.Config.Log.WithStack {
sti, ok := err.(interface{ StackTrace() errors.StackTrace }) if unwrap != err {
if ok { sti, ok := err.(interface{ StackTrace() errors.StackTrace })
log.ZWarn(ctx, "rpc server resp", err, "funcName", funcName, "unwrap", unwrap.Error(), "stack", fmt.Sprintf("%+v", err)) if ok {
if fs := sti.StackTrace(); len(fs) > 0 { log.ZWarn(ctx, "rpc server resp", err, "funcName", funcName, "unwrap", unwrap.Error(), "stack", fmt.Sprintf("%+v", err))
pc := uintptr(fs[0]) if fs := sti.StackTrace(); len(fs) > 0 {
fn := runtime.FuncForPC(pc) pc := uintptr(fs[0])
file, line := fn.FileLine(pc) fn := runtime.FuncForPC(pc)
errInfo = &errinfo.ErrorInfo{ file, line := fn.FileLine(pc)
Path: file, errInfo = &errinfo.ErrorInfo{
Line: uint32(line), Path: file,
Name: fn.Name(), Line: uint32(line),
Cause: unwrap.Error(), Name: fn.Name(),
Warp: nil, Cause: unwrap.Error(),
} Warp: nil,
if arr := strings.Split(err.Error(), ": "); len(arr) > 1 { }
errInfo.Warp = arr[:len(arr)-1] if arr := strings.Split(err.Error(), ": "); len(arr) > 1 {
errInfo.Warp = arr[:len(arr)-1]
}
} }
} }
} }

@ -2,13 +2,14 @@ package convert
import ( import (
"context" "context"
"time"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
discoveryRegistry "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" discoveryRegistry "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
sdk "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws" sdk "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/check" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/check"
utils2 "github.com/OpenIMSDK/Open-IM-Server/pkg/utils" utils2 "github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
utils "github.com/OpenIMSDK/open_utils" utils "github.com/OpenIMSDK/open_utils"
"time"
) )
type DBFriend struct { type DBFriend struct {
@ -110,20 +111,26 @@ func (db *DBFriendRequest) DB2PB(ctx context.Context, friendRequests []*relation
if len(friendRequests) > 0 { if len(friendRequests) > 0 {
userIDs = append(userIDs, friendRequests[0].FromUserID) userIDs = append(userIDs, friendRequests[0].FromUserID)
} }
for _, v := range friendRequests {
userIDs = append(userIDs, v.ToUserID)
}
users, err := db.userCheck.GetUsersInfoMap(ctx, userIDs, true) users, err := db.userCheck.GetUsersInfoMap(ctx, userIDs, true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, v := range friendRequests { for _, v := range friendRequests {
pbFriendRequest := &sdk.FriendRequest{} pbFriendRequest := &sdk.FriendRequest{}
pbFriendRequest.FromUserID = users[v.FromUserID].UserID
pbFriendRequest.FromNickname = users[v.FromUserID].Nickname pbFriendRequest.FromNickname = users[v.FromUserID].Nickname
pbFriendRequest.FromFaceURL = users[v.FromUserID].FaceURL pbFriendRequest.FromFaceURL = users[v.FromUserID].FaceURL
pbFriendRequest.FromGender = users[v.FromUserID].Gender pbFriendRequest.FromGender = users[v.FromUserID].Gender
pbFriendRequest.ToUserID = users[v.ToUserID].UserID
pbFriendRequest.ToNickname = users[v.ToUserID].Nickname pbFriendRequest.ToNickname = users[v.ToUserID].Nickname
pbFriendRequest.ToFaceURL = users[v.ToUserID].FaceURL pbFriendRequest.ToFaceURL = users[v.ToUserID].FaceURL
pbFriendRequest.ToGender = users[v.ToUserID].Gender pbFriendRequest.ToGender = users[v.ToUserID].Gender
pbFriendRequest.CreateTime = db.CreateTime.Unix() pbFriendRequest.CreateTime = v.CreateTime.Unix()
pbFriendRequest.HandleTime = db.HandleTime.Unix() pbFriendRequest.HandleTime = v.HandleTime.Unix()
pbFriendRequest.HandlerUserID = v.HandlerUserID
PBFriendRequests = append(PBFriendRequests, pbFriendRequest) PBFriendRequests = append(PBFriendRequests, pbFriendRequest)
} }
return return

@ -0,0 +1,10 @@
package utils
import "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
func GetPage(pagination *sdkws.RequestPagination) (pageNumber, showNumber int32) {
if pagination != nil {
return pagination.PageNumber, pagination.ShowNumber
}
return
}
Loading…
Cancel
Save