mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-06 10:05:58 +08:00
Merge branch 'errcode' of github.com:OpenIMSDK/Open-IM-Server into errcode
# Conflicts: # cmd/cmdutils/main.go
This commit is contained in:
@@ -1,92 +0,0 @@
|
||||
package constant
|
||||
|
||||
import (
|
||||
"OpenIM/pkg/utils"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ErrInfo struct {
|
||||
ErrCode int32
|
||||
ErrMsg string
|
||||
DetailErrMsg string
|
||||
}
|
||||
|
||||
func NewErrInfo(code int32, msg, detail string) *ErrInfo {
|
||||
return &ErrInfo{
|
||||
ErrCode: code,
|
||||
ErrMsg: msg,
|
||||
DetailErrMsg: detail,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *ErrInfo) Error() string {
|
||||
return "errMsg: " + e.ErrMsg + " detail errMsg: " + e.DetailErrMsg
|
||||
}
|
||||
|
||||
func (e *ErrInfo) Code() int32 {
|
||||
return e.ErrCode
|
||||
}
|
||||
|
||||
func (e *ErrInfo) Msg() string {
|
||||
return e.ErrMsg
|
||||
}
|
||||
|
||||
func (e *ErrInfo) Detail() string {
|
||||
return e.DetailErrMsg
|
||||
}
|
||||
|
||||
func (e *ErrInfo) Wrap(msg ...string) error {
|
||||
return errors.Wrap(e, strings.Join(msg, "--"))
|
||||
}
|
||||
|
||||
func NewErrNetwork(err error) error {
|
||||
return toDetail(err, ErrNetwork)
|
||||
}
|
||||
|
||||
func NewErrData(err error) error {
|
||||
return toDetail(err, ErrData)
|
||||
}
|
||||
|
||||
func toDetail(err error, info *ErrInfo) *ErrInfo {
|
||||
errInfo := *info
|
||||
errInfo.DetailErrMsg = err.Error()
|
||||
return &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)
|
||||
}
|
||||
@@ -1,167 +0,0 @@
|
||||
package constant
|
||||
|
||||
var (
|
||||
ErrNone = &ErrInfo{0, "", ""}
|
||||
ErrArgs = &ErrInfo{ArgsError, "ArgsError", ""}
|
||||
ErrDatabase = &ErrInfo{DatabaseError, "DatabaseError", ""}
|
||||
ErrInternalServer = &ErrInfo{ServerInternalError, "ServerInternalError", ""}
|
||||
ErrNetwork = &ErrInfo{NetworkError, "NetworkError", ""}
|
||||
ErrNoPermission = &ErrInfo{NoPermissionError, "NoPermissionError", ""}
|
||||
ErrIdentity = &ErrInfo{IdentityError, "IdentityError", ""}
|
||||
ErrCallback = &ErrInfo{CallbackError, "CallbackError", ""}
|
||||
ErrCallbackContinue = &ErrInfo{ErrMsg: "CallbackContinueError"}
|
||||
|
||||
ErrUserIDNotFound = &ErrInfo{UserIDNotFoundError, "UserIDNotFoundError", ""}
|
||||
ErrGroupIDNotFound = &ErrInfo{GroupIDNotFoundError, "GroupIDNotFoundError", ""}
|
||||
ErrGroupIDExisted = &ErrInfo{GroupIDNotFoundError, "GroupIDExisted", ""} // todo group id 已存在
|
||||
|
||||
ErrRecordNotFound = &ErrInfo{RecordNotFoundError, "RecordNotFoundError", ""}
|
||||
|
||||
ErrRelationshipAlready = &ErrInfo{RelationshipAlreadyError, "RelationshipAlreadyError", ""}
|
||||
ErrNotRelationshipYet = &ErrInfo{NotRelationshipYetError, "NotRelationshipYetError", ""}
|
||||
ErrCanNotAddYourself = &ErrInfo{CanNotAddYourselfError, "CanNotAddYourselfError", ""}
|
||||
|
||||
ErrOnlyOneOwner = &ErrInfo{OnlyOneOwnerError, "OnlyOneOwnerError", ""}
|
||||
ErrInGroupAlready = &ErrInfo{InGroupAlreadyError, "InGroupAlreadyError", ""}
|
||||
ErrNotInGroupYet = &ErrInfo{NotInGroupYetError, "NotInGroupYetError", ""}
|
||||
ErrDismissedAlready = &ErrInfo{DismissedAlreadyError, "DismissedAlreadyError", ""}
|
||||
ErrOwnerNotAllowedQuit = &ErrInfo{OwnerNotAllowedQuitError, "OwnerNotAllowedQuitError", ""}
|
||||
ErrRegisteredAlready = &ErrInfo{RegisteredAlreadyError, "RegisteredAlreadyError", ""}
|
||||
ErrGroupTypeNotSupport = &ErrInfo{GroupTypeNotSupport, "", ""}
|
||||
ErrGroupNoOwner = &ErrInfo{GroupNoOwner, "ErrGroupNoOwner", ""}
|
||||
|
||||
ErrDefaultOther = &ErrInfo{DefaultOtherError, "DefaultOtherError", ""}
|
||||
ErrData = &ErrInfo{DataError, "DataError", ""}
|
||||
ErrTokenExpired = &ErrInfo{TokenExpiredError, "TokenExpiredError", ""}
|
||||
ErrTokenInvalid = &ErrInfo{TokenInvalidError, "TokenInvalidError", ""} //
|
||||
ErrTokenMalformed = &ErrInfo{TokenMalformedError, "TokenMalformedError", ""} //格式错误
|
||||
ErrTokenNotValidYet = &ErrInfo{TokenNotValidYetError, "TokenNotValidYetError", ""} //还未生效
|
||||
ErrTokenUnknown = &ErrInfo{TokenUnknownError, "TokenUnknownError", ""} //未知错误
|
||||
ErrTokenKicked = &ErrInfo{TokenKickedError, "TokenKickedError", ""}
|
||||
ErrTokenNotExist = &ErrInfo{TokenNotExistError, "TokenNotExistError", ""} //在redis中不存在
|
||||
ErrTokenDifferentPlatformID = &ErrInfo{TokenDifferentPlatformIDError, "TokenDifferentPlatformIDError", ""}
|
||||
ErrTokenDifferentUserID = &ErrInfo{TokenDifferentUserIDError, "TokenDifferentUserIDError", ""}
|
||||
|
||||
ErrMessageHasReadDisable = &ErrInfo{MessageHasReadDisable, "MessageHasReadDisable", ""}
|
||||
|
||||
ErrDB = ErrDatabase
|
||||
ErrSendLimit = ErrInternalServer
|
||||
|
||||
ErrBlockedByPeer = &ErrInfo{BlockedByPeer, "BlockedByPeer", ""}
|
||||
//不是对方的好友
|
||||
ErrNotPeersFriend = &ErrInfo{NotPeersFriend, "NotPeersFriend", ""}
|
||||
//
|
||||
ErrMutedInGroup = &ErrInfo{MutedInGroup, "MutedInGroup", ""}
|
||||
ErrMutedGroup = &ErrInfo{MutedGroup, "MutedGroup", ""}
|
||||
|
||||
ErrConnOverMaxNumLimit = &ErrInfo{ConnOverMaxNumLimit, "ConnOverMaxNumLimit", ""}
|
||||
|
||||
ErrConnArgsErr = &ErrInfo{ConnArgsErr, "args err, need token, sendID, platformID", ""}
|
||||
ErrConnUpdateErr = &ErrInfo{ConnArgsErr, "upgrade http conn err", ""}
|
||||
|
||||
ErrConfig = &ErrInfo{ConfigError, "ConfigError", ""}
|
||||
)
|
||||
|
||||
const (
|
||||
FormattingError = 10001
|
||||
HasRegistered = 10002
|
||||
NotRegistered = 10003
|
||||
PasswordErr = 10004
|
||||
GetIMTokenErr = 10005
|
||||
RepeatSendCode = 10006
|
||||
MailSendCodeErr = 10007
|
||||
SmsSendCodeErr = 10008
|
||||
CodeInvalidOrExpired = 10009
|
||||
RegisterFailed = 10010
|
||||
ResetPasswordFailed = 10011
|
||||
RegisterLimit = 10012
|
||||
LoginLimit = 10013
|
||||
InvitationError = 10014
|
||||
)
|
||||
|
||||
// 通用错误码
|
||||
const (
|
||||
NoError = 0 //无错误
|
||||
ArgsError = 90001 //输入参数错误
|
||||
DatabaseError = 90002 //redis/mysql等db错误
|
||||
ServerInternalError = 90003 //服务器内部错误
|
||||
NetworkError = 90004 //网络错误
|
||||
NoPermissionError = 90005 //权限不足
|
||||
GRPCConnIsNil = 90006 //grpc连接空
|
||||
|
||||
DefaultOtherError = 90006 //其他错误
|
||||
DataError = 90007 //数据错误
|
||||
|
||||
IdentityError = 90008 // 身份错误 非管理员token,且token中userID与请求userID不一致
|
||||
|
||||
ConfigError = 90009
|
||||
|
||||
CallbackError = 80000
|
||||
)
|
||||
|
||||
// 账号错误码
|
||||
const (
|
||||
UserIDNotFoundError = 91001 //UserID不存在 或未注册
|
||||
GroupIDNotFoundError = 91002 //GroupID不存在
|
||||
RecordNotFoundError = 91002 //记录不存在
|
||||
)
|
||||
|
||||
// 关系链错误码
|
||||
const (
|
||||
RelationshipAlreadyError = 92001 //已经是好友关系(或者黑名单)
|
||||
NotRelationshipYetError = 92002 //不是好友关系(或者黑名单)
|
||||
CanNotAddYourselfError = 92003 //不能添加自己为好友
|
||||
BlockedByPeer = 92004 //被对方拉黑
|
||||
NotPeersFriend = 92005 //不是对方的好友
|
||||
)
|
||||
|
||||
// 群组错误码
|
||||
const (
|
||||
OnlyOneOwnerError = 93001 //只能有一个群主
|
||||
InGroupAlreadyError = 93003 //已在群组中
|
||||
NotInGroupYetError = 93004 //不在群组中
|
||||
DismissedAlreadyError = 93004 //群组已经解散
|
||||
OwnerNotAllowedQuitError = 93004 //群主不能退群
|
||||
GroupTypeNotSupport = 93005
|
||||
GroupNoOwner = 93006
|
||||
|
||||
MutedInGroup = 93007 //群成员被禁言
|
||||
MutedGroup = 93008 //群被禁言
|
||||
)
|
||||
|
||||
// 用户错误码
|
||||
const (
|
||||
RegisteredAlreadyError = 94001 //用户已经注册过了
|
||||
)
|
||||
|
||||
// token错误码
|
||||
const (
|
||||
TokenExpiredError = 95001
|
||||
TokenInvalidError = 95002
|
||||
TokenMalformedError = 95003
|
||||
TokenNotValidYetError = 95004
|
||||
TokenUnknownError = 95005
|
||||
TokenKickedError = 95006
|
||||
TokenDifferentPlatformIDError = 95007
|
||||
TokenDifferentUserIDError = 95008
|
||||
TokenNotExistError = 95009
|
||||
)
|
||||
|
||||
// 消息错误码
|
||||
const (
|
||||
MessageHasReadDisable = 96001
|
||||
)
|
||||
|
||||
// 长连接网关错误码
|
||||
const (
|
||||
ConnOverMaxNumLimit = 970001
|
||||
ConnArgsErr = 970002
|
||||
ConnUpdateErr = 970003
|
||||
)
|
||||
|
||||
// temp
|
||||
|
||||
var (
|
||||
ErrServer = &ErrInfo{500, "server error", ""}
|
||||
ErrTencentCredential = &ErrInfo{400, "ErrTencentCredential", ""}
|
||||
)
|
||||
@@ -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,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,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 {
|
||||
|
||||
@@ -9,7 +9,7 @@ package http
|
||||
import (
|
||||
"OpenIM/pkg/callbackstruct"
|
||||
"OpenIM/pkg/common/config"
|
||||
"OpenIM/pkg/common/constant"
|
||||
"OpenIM/pkg/errs"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
@@ -76,15 +76,15 @@ 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)
|
||||
return errs.ErrNetwork.Wrap(err.Error())
|
||||
}
|
||||
if err = json.Unmarshal(b, output); err != nil {
|
||||
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
|
||||
return constant.ErrCallbackContinue
|
||||
return errs.ErrCallbackContinue
|
||||
}
|
||||
return constant.NewErrData(err)
|
||||
return errs.ErrData.Wrap(err.Error())
|
||||
}
|
||||
return output.Parse()
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package middleware
|
||||
package mw
|
||||
|
||||
import (
|
||||
"OpenIM/pkg/common/constant"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
@@ -26,36 +26,38 @@ func CorsHandler() gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func GinParseOperationID(c *gin.Context) {
|
||||
if c.Request.Method == http.MethodPost {
|
||||
operationID := c.Request.Header.Get(constant.OperationID)
|
||||
if operationID == "" {
|
||||
body, err := ioutil.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
c.String(400, "read request body error: "+err.Error())
|
||||
c.Abort()
|
||||
return
|
||||
func GinParseOperationID() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if c.Request.Method == http.MethodPost {
|
||||
operationID := c.Request.Header.Get(constant.OperationID)
|
||||
if operationID == "" {
|
||||
body, err := io.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
c.String(400, "read request body error: "+err.Error())
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
req := struct {
|
||||
OperationID string `json:"operationID"`
|
||||
}{}
|
||||
if err := json.Unmarshal(body, &req); err != nil {
|
||||
c.String(400, "get operationID error: "+err.Error())
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if req.OperationID == "" {
|
||||
c.String(400, "operationID empty")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Request.Body = io.NopCloser(bytes.NewReader(body))
|
||||
operationID = req.OperationID
|
||||
c.Request.Header.Set(constant.OperationID, operationID)
|
||||
}
|
||||
req := struct {
|
||||
OperationID string `json:"operationID"`
|
||||
}{}
|
||||
if err := json.Unmarshal(body, &req); err != nil {
|
||||
c.String(400, "get operationID error: "+err.Error())
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if req.OperationID == "" {
|
||||
c.String(400, "operationID empty")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Request.Body = ioutil.NopCloser(bytes.NewReader(body))
|
||||
operationID = req.OperationID
|
||||
c.Request.Header.Set(constant.OperationID, operationID)
|
||||
c.Set(constant.OperationID, operationID)
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
c.Set(constant.OperationID, operationID)
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package middleware
|
||||
package mw
|
||||
|
||||
import (
|
||||
"OpenIM/pkg/common/constant"
|
||||
"OpenIM/pkg/common/log"
|
||||
"OpenIM/pkg/common/tracelog"
|
||||
"OpenIM/pkg/utils"
|
||||
"OpenIM/pkg/errs"
|
||||
"context"
|
||||
"fmt"
|
||||
"google.golang.org/grpc"
|
||||
@@ -15,14 +15,13 @@ import (
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
func RpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
||||
func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
||||
var operationID string
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.NewError(operationID, info.FullMethod, "type:", fmt.Sprintf("%T", r), "panic:", r, "stack:", string(debug.Stack()))
|
||||
}
|
||||
}()
|
||||
//funcName := path.Base(info.FullMethod)
|
||||
funcName := info.FullMethod
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
@@ -34,7 +33,7 @@ func RpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary
|
||||
operationID = opts[0]
|
||||
}
|
||||
var opUserID string
|
||||
if opts := md.Get("opUserID"); len(opts) == 1 {
|
||||
if opts := md.Get(constant.OpUserID); len(opts) == 1 {
|
||||
opUserID = opts[0]
|
||||
}
|
||||
ctx = tracelog.NewRpcCtx(ctx, funcName, operationID)
|
||||
@@ -43,42 +42,51 @@ 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 {
|
||||
code = codes.Unknown
|
||||
} else {
|
||||
code = codes.Code(errInfo.ErrCode)
|
||||
}
|
||||
sta, err := status.New(code, errInfo.ErrMsg).WithDetails(wrapperspb.String(errInfo.DetailErrMsg))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, sta.Err()
|
||||
return nil, rpcErrorToCode(err).Err()
|
||||
}
|
||||
tracelog.SetCtxInfo(ctx, funcName, nil, "rpcResp", rpcString(resp))
|
||||
return
|
||||
}
|
||||
|
||||
func rpcString(v interface{}) string {
|
||||
if s, ok := v.(interface{ String() string }); ok {
|
||||
return s.String()
|
||||
func rpcClientInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) (err error) {
|
||||
if ctx == nil {
|
||||
return errs.ErrInternalServer.Wrap("call rpc request context is nil")
|
||||
}
|
||||
return fmt.Sprintf("%+v", v)
|
||||
}
|
||||
|
||||
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, "")
|
||||
//}
|
||||
operationID, ok := ctx.Value(constant.OperationID).(string)
|
||||
if !ok {
|
||||
return utils.Wrap(constant.ErrArgs, "ctx missing operationID")
|
||||
return errs.ErrArgs.Wrap("ctx missing operationID")
|
||||
}
|
||||
opUserID, ok := ctx.Value("opUserID").(string)
|
||||
md := metadata.Pairs(constant.OperationID, operationID)
|
||||
opUserID, ok := ctx.Value(constant.OpUserID).(string)
|
||||
if ok {
|
||||
md.Append(constant.OpUserID, opUserID)
|
||||
}
|
||||
err = invoker(metadata.NewOutgoingContext(ctx, md), method, req, reply, cc, opts...)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
rpcErr, ok := err.(interface{ GRPCStatus() *status.Status })
|
||||
if !ok {
|
||||
return utils.Wrap(constant.ErrArgs, "ctx missing opUserID")
|
||||
return errs.NewCodeError(errs.DefaultOtherError, err.Error()).Wrap()
|
||||
}
|
||||
md := metadata.Pairs(constant.OperationID, operationID, "opUserID", opUserID)
|
||||
return invoker(metadata.NewOutgoingContext(ctx, md), method, req, reply, cc, opts...)
|
||||
sta := rpcErr.GRPCStatus()
|
||||
if sta.Code() == 0 {
|
||||
return errs.NewCodeError(errs.DefaultOtherError, err.Error()).Wrap()
|
||||
}
|
||||
details := sta.Details()
|
||||
if len(details) == 0 {
|
||||
return errs.NewCodeError(int(sta.Code()), sta.Message()).Wrap()
|
||||
}
|
||||
if v, ok := details[0].(*wrapperspb.StringValue); ok {
|
||||
return errs.NewCodeError(int(sta.Code()), sta.Message()).Wrap(v.String())
|
||||
}
|
||||
return errs.NewCodeError(int(sta.Code()), sta.Message()).Wrap()
|
||||
}
|
||||
|
||||
func GrpcServer() grpc.ServerOption {
|
||||
return grpc.UnaryInterceptor(rpcServerInterceptor)
|
||||
}
|
||||
|
||||
func GrpcClient() grpc.DialOption {
|
||||
return grpc.WithUnaryInterceptor(rpcClientInterceptor)
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package mw
|
||||
|
||||
import (
|
||||
"OpenIM/pkg/errs"
|
||||
"fmt"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/types/known/wrapperspb"
|
||||
"math"
|
||||
)
|
||||
|
||||
func rpcString(v interface{}) string {
|
||||
if s, ok := v.(interface{ String() string }); ok {
|
||||
return s.String()
|
||||
}
|
||||
return fmt.Sprintf("%+v", v)
|
||||
}
|
||||
|
||||
func rpcErrorToCode(err error) *status.Status {
|
||||
unwrap := errs.Unwrap(err)
|
||||
var (
|
||||
code codes.Code
|
||||
msg string
|
||||
)
|
||||
if unwrap.(errs.CodeError) != nil {
|
||||
c := unwrap.(errs.CodeError).Code()
|
||||
if c <= 0 || c > math.MaxUint32 {
|
||||
code = codes.OutOfRange // 错误码超出范围
|
||||
} else {
|
||||
code = codes.Code(c)
|
||||
}
|
||||
msg = unwrap.(errs.CodeError).Msg()
|
||||
} else {
|
||||
code = codes.Unknown
|
||||
msg = unwrap.Error()
|
||||
}
|
||||
sta := status.New(code, msg)
|
||||
if unwrap == err {
|
||||
return sta
|
||||
}
|
||||
details, err := sta.WithDetails(wrapperspb.String(fmt.Sprintf("%+v", err)))
|
||||
if err != nil {
|
||||
return sta
|
||||
}
|
||||
return details
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user