mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-06 10:05:58 +08:00
errcode
This commit is contained in:
@@ -10,11 +10,6 @@ func GetContent(msg *server_api_params.MsgData) string {
|
||||
if msg.ContentType >= constant.NotificationBegin && msg.ContentType <= constant.NotificationEnd {
|
||||
var tips server_api_params.TipsComm
|
||||
_ = proto.Unmarshal(msg.Content, &tips)
|
||||
//marshaler := jsonpb.Marshaler{
|
||||
// OrigName: true,
|
||||
// EnumsAsInts: false,
|
||||
// EmitDefaults: false,
|
||||
//}
|
||||
content := tips.JsonDetail
|
||||
return content
|
||||
} else {
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
commonDB "Open_IM/pkg/common/db"
|
||||
"Open_IM/pkg/common/log"
|
||||
"Open_IM/pkg/common/tools"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
@@ -160,7 +159,7 @@ func CheckAccess(ctx context.Context, OpUserID string, OwnerUserID string) bool
|
||||
}
|
||||
|
||||
func CheckAccessV3(ctx context.Context, ownerUserID string) (err error) {
|
||||
opUserID := tools.OpUserID(ctx)
|
||||
opUserID := utils.OpUserID(ctx)
|
||||
defer func() {
|
||||
tracelog.SetCtxInfo(ctx, utils.GetFuncName(1), err, "OpUserID", opUserID, "ownerUserID", ownerUserID)
|
||||
}()
|
||||
@@ -174,11 +173,11 @@ func CheckAccessV3(ctx context.Context, ownerUserID string) (err error) {
|
||||
}
|
||||
|
||||
func IsAppManagerUid(ctx context.Context) bool {
|
||||
return utils.IsContain(tools.OpUserID(ctx), config.Config.Manager.AppManagerUid)
|
||||
return utils.IsContain(utils.OpUserID(ctx), config.Config.Manager.AppManagerUid)
|
||||
}
|
||||
|
||||
func CheckAdmin(ctx context.Context) error {
|
||||
if utils.IsContain(tools.OpUserID(ctx), config.Config.Manager.AppManagerUid) {
|
||||
if utils.IsContain(utils.OpUserID(ctx), config.Config.Manager.AppManagerUid) {
|
||||
return nil
|
||||
}
|
||||
return constant.ErrIdentity.Wrap()
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
package tools
|
||||
|
||||
import "context"
|
||||
|
||||
func OperationID(ctx context.Context) string {
|
||||
s, _ := ctx.Value("operationID").(string)
|
||||
return s
|
||||
}
|
||||
|
||||
func OpUserID(ctx context.Context) string {
|
||||
s, _ := ctx.Value("opUserID").(string)
|
||||
return s
|
||||
}
|
||||
@@ -1,28 +1,20 @@
|
||||
package tracelog
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"github.com/sirupsen/logrus"
|
||||
"google.golang.org/grpc/status"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
//"errors"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const TraceLogKey = "tracelog"
|
||||
|
||||
func NewCtx(c *gin.Context, api string) context.Context {
|
||||
req := &ApiInfo{ApiName: api, GinCtx: c, Funcs: &[]FuncInfo{}}
|
||||
return context.WithValue(c, TraceLogKey, req)
|
||||
}
|
||||
|
||||
func NewCtx1(c *gin.Context, api string) context.Context {
|
||||
req := &ApiInfo{ApiName: api, GinCtx: c, OperationID: c.GetHeader("operationID"), Funcs: &[]FuncInfo{}}
|
||||
return context.WithValue(c, TraceLogKey, req)
|
||||
}
|
||||
@@ -42,77 +34,11 @@ func GetOperationID(ctx context.Context) string {
|
||||
return ctx.Value(TraceLogKey).(*ApiInfo).OperationID
|
||||
}
|
||||
|
||||
//func ShowLog(ctx context.Context) {
|
||||
// t := ctx.Value(TraceLogKey).(*ApiInfo)
|
||||
// if ctx.Value(TraceLogKey).(*ApiInfo).GinCtx != nil {
|
||||
// log.Info(t.OperationID, "api: ", t.ApiName)
|
||||
// } else {
|
||||
// log.Info(t.OperationID, "rpc: ", t.ApiName)
|
||||
// }
|
||||
// for _, v := range *t.Funcs {
|
||||
// if v.Err != nil {
|
||||
// log.Error(t.OperationID, "func: ", v.FuncName, " args: ", v.Args, v.Err.Error())
|
||||
// } else {
|
||||
// switch v.LogLevel {
|
||||
// case logrus.InfoLevel:
|
||||
// log.Info(t.OperationID, "func: ", v.FuncName, " args: ", v.Args)
|
||||
// case logrus.DebugLevel:
|
||||
// log.Debug(t.OperationID, "func: ", v.FuncName, " args: ", v.Args)
|
||||
// case logrus.WarnLevel:
|
||||
// log.Debug(t.OperationID, "func: ", v.FuncName, " args: ", v.Args)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
func WriteErrorResponse(ctx context.Context, funcName string, err error, args ...interface{}) {
|
||||
SetCtxInfo(ctx, funcName, err, args)
|
||||
e := Unwrap(err)
|
||||
switch t := e.(type) {
|
||||
case *constant.ErrInfo:
|
||||
ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, baseResp{ErrCode: t.ErrCode, ErrMsg: t.ErrMsg, ErrDtl: t.DetailErrMsg})
|
||||
//ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": t.ErrCode, "errMsg": t.ErrMsg, "errDtl": t.DetailErrMsg})
|
||||
return
|
||||
default:
|
||||
s, ok := status.FromError(e)
|
||||
if !ok {
|
||||
ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, &baseResp{ErrCode: constant.ErrDefaultOther.ErrCode, ErrMsg: err.Error(), ErrDtl: fmt.Sprintf("%+v", err)})
|
||||
//ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": constant.ErrDefaultOther.ErrCode, "errMsg": err.Error(), "errDtl": fmt.Sprintf("%+v", err)})
|
||||
return
|
||||
}
|
||||
var details []string
|
||||
if err != e {
|
||||
details = append(details, fmt.Sprintf("%+v", err))
|
||||
}
|
||||
for _, s := range s.Details() {
|
||||
details = append(details, fmt.Sprintf("%+v", s))
|
||||
}
|
||||
ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, &baseResp{ErrCode: int32(s.Code()), ErrMsg: s.Message(), ErrDtl: strings.Join(details, "\n")})
|
||||
//ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": s.Code(), "errMsg": s.Message(), "errDtl": strings.Join(details, "\n")})
|
||||
return
|
||||
}
|
||||
func GetOpUserID(ctx context.Context) string {
|
||||
s, _ := ctx.Value("opUserID").(string)
|
||||
return s
|
||||
}
|
||||
|
||||
type baseResp struct {
|
||||
ErrCode int32 `json:"errCode"`
|
||||
ErrMsg string `json:"errMsg"`
|
||||
ErrDtl string `json:"errDtl"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
//func WriteErrorResponse(ctx context.Context, funcName string, err error, args ...interface{}) {
|
||||
// SetCtxInfo(ctx, funcName, err, args)
|
||||
// e := new(constant.ErrInfo)
|
||||
// switch {
|
||||
// case errors.As(err, &e):
|
||||
// ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": e.ErrCode, "errMsg": e.ErrMsg})
|
||||
// return
|
||||
// default:
|
||||
// ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": constant.ErrDefaultOther.ErrCode, "errMsg": constant.ErrDefaultOther.ErrMsg, "errDtl": err.Error()})
|
||||
// return
|
||||
// }
|
||||
//}
|
||||
|
||||
func Unwrap(err error) error {
|
||||
for err != nil {
|
||||
unwrap, ok := err.(interface {
|
||||
@@ -211,11 +137,6 @@ func SetRpcRespInfo(ctx context.Context, funcName string, resp string) {
|
||||
*t.Funcs = append(*t.Funcs, funcInfo)
|
||||
}
|
||||
|
||||
func SetSuccess(ctx context.Context, funcName string, data interface{}) {
|
||||
SetCtxInfo(ctx, funcName, nil, "data", data)
|
||||
ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": "", "errDtl": "", "data": data})
|
||||
}
|
||||
|
||||
func argsHandle(args []interface{}, fields map[string]interface{}) {
|
||||
for i := 0; i < len(args); i += 2 {
|
||||
if i+1 < len(args) {
|
||||
|
||||
Reference in New Issue
Block a user