This commit is contained in:
withchao
2023-03-07 12:19:30 +08:00
parent 3fe9ee22fe
commit 4c5613084c
40 changed files with 315 additions and 297 deletions
+32 -35
View File
@@ -1,11 +1,7 @@
package constant
import (
"OpenIM/pkg/utils"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"gorm.io/gorm"
"strings"
)
@@ -58,35 +54,36 @@ func toDetail(err error, info *ErrInfo) *ErrInfo {
}
func ToAPIErrWithErr(err error) *ErrInfo {
unwrap := utils.Unwrap(err)
if unwrap == gorm.ErrRecordNotFound {
return &ErrInfo{
ErrCode: ErrRecordNotFound.Code(),
ErrMsg: ErrRecordNotFound.Msg(),
DetailErrMsg: fmt.Sprintf("%+v", err),
}
}
if errInfo, ok := unwrap.(*ErrInfo); ok {
return &ErrInfo{
ErrCode: errInfo.Code(),
ErrMsg: errInfo.Msg(),
DetailErrMsg: fmt.Sprintf("%+v", err),
}
}
errComm := errors.New("")
var marshalErr *json.MarshalerError
errInfo := &ErrInfo{}
switch {
case errors.As(err, &errComm):
if errors.Is(err, gorm.ErrRecordNotFound) {
return toDetail(err, ErrRecordNotFound)
}
return toDetail(err, ErrData)
case errors.As(err, &marshalErr):
return toDetail(err, ErrData)
case errors.As(err, &errInfo):
return toDetail(err, errInfo)
}
return toDetail(err, ErrDefaultOther)
return &ErrInfo{}
//unwrap := utils.Unwrap(err)
//if unwrap == gorm.ErrRecordNotFound {
// return &ErrInfo{
// ErrCode: ErrRecordNotFound.Code(),
// ErrMsg: ErrRecordNotFound.Msg(),
// DetailErrMsg: fmt.Sprintf("%+v", err),
// }
//}
//if errInfo, ok := unwrap.(*ErrInfo); ok {
// return &ErrInfo{
// ErrCode: errInfo.Code(),
// ErrMsg: errInfo.Msg(),
// DetailErrMsg: fmt.Sprintf("%+v", err),
// }
//}
//
//errComm := errors.New("")
//var marshalErr *json.MarshalerError
//errInfo := &ErrInfo{}
//switch {
//case errors.As(err, &errComm):
// if errors.Is(err, gorm.ErrRecordNotFound) {
// return toDetail(err, ErrRecordNotFound)
// }
// return toDetail(err, ErrData)
//case errors.As(err, &marshalErr):
// return toDetail(err, ErrData)
//case errors.As(err, &errInfo):
// return toDetail(err, errInfo)
//}
//return toDetail(err, ErrDefaultOther)
}
+2 -1
View File
@@ -4,6 +4,7 @@ import (
"OpenIM/pkg/common/constant"
"OpenIM/pkg/common/db/table/relation"
"OpenIM/pkg/common/db/tx"
"OpenIM/pkg/errs"
"OpenIM/pkg/utils"
"context"
"errors"
@@ -235,7 +236,7 @@ func (f *friendDatabase) FindFriendsWithError(ctx context.Context, ownerUserID s
return
}
if len(friends) != len(friendUserIDs) {
err = constant.ErrRecordNotFound.Wrap()
err = errs.ErrRecordNotFound.Wrap()
}
return
}
+3 -2
View File
@@ -3,6 +3,7 @@ package controller
import (
"OpenIM/pkg/common/constant"
"OpenIM/pkg/common/db/table/relation"
"OpenIM/pkg/errs"
"OpenIM/pkg/utils"
"context"
)
@@ -60,7 +61,7 @@ func (u *userDatabase) FindWithError(ctx context.Context, userIDs []string) (use
return
}
if len(users) != len(userIDs) {
err = constant.ErrRecordNotFound.Wrap()
err = errs.ErrRecordNotFound.Wrap()
}
return
}
@@ -114,7 +115,7 @@ func (u *userDatabase) GetAllUserID(ctx context.Context) (userIDs []string, err
if total == int64(len(userIDs)) {
return userIDs, nil
}
return nil, constant.ErrData.Wrap("The total number of results and expectations are different, but result is nil")
return nil, errs.ErrData.Wrap("The total number of results and expectations are different, but result is nil")
}
userIDs = append(userIDs, tmp...)
pageNumber++
+2 -2
View File
@@ -2,8 +2,8 @@ package localcache
import (
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
"OpenIM/pkg/discoveryregistry"
"OpenIM/pkg/errs"
"OpenIM/pkg/proto/group"
"context"
"sync"
@@ -46,7 +46,7 @@ func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string)
return nil, err
}
if len(resp.GroupAbstractInfos) < 0 {
return nil, constant.ErrGroupIDNotFound
return nil, errs.ErrGroupIDNotFound
}
localHashInfo, ok := g.cache[groupID]
if ok && localHashInfo.memberListHash == resp.GroupAbstractInfos[0].GroupMemberListHash {
+3 -2
View File
@@ -10,6 +10,7 @@ import (
"OpenIM/pkg/callbackstruct"
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
"OpenIM/pkg/errs"
"bytes"
"encoding/json"
"io/ioutil"
@@ -76,13 +77,13 @@ func callBackPostReturn(url, command string, input interface{}, output callbacks
b, err := Post(url, nil, input, callbackConfig.CallbackTimeOut)
if err != nil {
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
return constant.ErrCallbackContinue
return errs.ErrCallbackContinue
}
return constant.NewErrNetwork(err)
}
if err = json.Unmarshal(b, output); err != nil {
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
return constant.ErrCallbackContinue
return errs.ErrCallbackContinue
}
return constant.NewErrData(err)
}
+5 -3
View File
@@ -4,6 +4,7 @@ import (
"OpenIM/pkg/common/constant"
"OpenIM/pkg/common/log"
"OpenIM/pkg/common/tracelog"
"OpenIM/pkg/errs"
"OpenIM/pkg/utils"
"context"
"fmt"
@@ -43,6 +44,7 @@ func RpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary
resp, err = handler(ctx, req)
if err != nil {
tracelog.SetCtxInfo(ctx, funcName, err)
errInfo := constant.ToAPIErrWithErr(err)
var code codes.Code
if errInfo.ErrCode == 0 {
@@ -69,15 +71,15 @@ func rpcString(v interface{}) string {
func RpcClientInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) (err error) {
//if cc == nil {
// return utils.Wrap(constant.ErrRpcConn, "")
// return utils.Wrap(errs.ErrRpcConn, "")
//}
operationID, ok := ctx.Value(constant.OperationID).(string)
if !ok {
return utils.Wrap(constant.ErrArgs, "ctx missing operationID")
return utils.Wrap(errs.ErrArgs, "ctx missing operationID")
}
opUserID, ok := ctx.Value("opUserID").(string)
if !ok {
return utils.Wrap(constant.ErrArgs, "ctx missing opUserID")
return utils.Wrap(errs.ErrArgs, "ctx missing opUserID")
}
md := metadata.Pairs(constant.OperationID, operationID, "opUserID", opUserID)
return invoker(metadata.NewOutgoingContext(ctx, md), method, req, reply, cc, opts...)
+9 -9
View File
@@ -2,8 +2,8 @@ package tokenverify
import (
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
"OpenIM/pkg/common/tracelog"
"OpenIM/pkg/errs"
"OpenIM/pkg/utils"
"context"
"github.com/golang-jwt/jwt/v4"
@@ -40,22 +40,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(errs.ErrTokenMalformed, "")
} else if ve.Errors&jwt.ValidationErrorExpired != 0 {
return nil, utils.Wrap(constant.ErrTokenExpired, "")
return nil, utils.Wrap(errs.ErrTokenExpired, "")
} else if ve.Errors&jwt.ValidationErrorNotValidYet != 0 {
return nil, utils.Wrap(constant.ErrTokenNotValidYet, "")
return nil, utils.Wrap(errs.ErrTokenNotValidYet, "")
} else {
return nil, utils.Wrap(constant.ErrTokenUnknown, "")
return nil, utils.Wrap(errs.ErrTokenUnknown, "")
}
} else {
return nil, utils.Wrap(constant.ErrTokenUnknown, "")
return nil, utils.Wrap(errs.ErrTokenUnknown, "")
}
} else {
if claims, ok := token.Claims.(*Claims); ok && token.Valid {
return claims, nil
}
return nil, utils.Wrap(constant.ErrTokenUnknown, "")
return nil, utils.Wrap(errs.ErrTokenUnknown, "")
}
}
@@ -70,7 +70,7 @@ func CheckAccessV3(ctx context.Context, ownerUserID string) (err error) {
if opUserID == ownerUserID {
return nil
}
return constant.ErrIdentity.Wrap(utils.GetSelfFuncName())
return errs.ErrIdentity.Wrap(utils.GetSelfFuncName())
}
func IsAppManagerUid(ctx context.Context) bool {
@@ -81,7 +81,7 @@ func CheckAdmin(ctx context.Context) error {
if utils.IsContain(tracelog.GetOpUserID(ctx), config.Config.Manager.AppManagerUid) {
return nil
}
return constant.ErrIdentity.Wrap()
return errs.ErrIdentity.Wrap()
}
func ParseRedisInterfaceToken(redisToken interface{}) (*Claims, error) {