This commit is contained in:
wangchuxiao
2023-01-30 15:28:46 +08:00
parent 26ed14d1d3
commit 67c082ab7b
90 changed files with 293 additions and 489 deletions
+49 -5
View File
@@ -1,6 +1,7 @@
package common
import (
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/log"
"Open_IM/pkg/common/tracelog"
"Open_IM/pkg/getcdv3"
@@ -8,7 +9,10 @@ import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"google.golang.org/grpc/status"
"net/http"
"reflect"
"strings"
)
func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, rpcClientFunc interface{}, rpcFuncName string) {
@@ -16,16 +20,16 @@ func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, rpcCl
rpcName = utils2.GetFuncName(1)
}
logFuncName := fmt.Sprintf("[ApiToRpc: %s]%s", utils2.GetFuncName(1), rpcFuncName)
ctx := tracelog.NewCtx1(c, rpcFuncName)
ctx := tracelog.NewCtx(c, rpcFuncName)
defer log.ShowLog(ctx)
if err := c.BindJSON(apiReq); err != nil {
tracelog.WriteErrorResponse(ctx, "BindJSON", err)
WriteErrorResponse(ctx, "BindJSON", err)
return
}
tracelog.SetCtxInfo(ctx, logFuncName, nil, "apiReq", apiReq)
etcdConn, err := getcdv3.GetConn(ctx, rpcName)
if err != nil {
tracelog.WriteErrorResponse(ctx, "GetConn", err)
WriteErrorResponse(ctx, "GetConn", err)
return
}
rpcClient := reflect.ValueOf(rpcClientFunc).Call([]reflect.Value{
@@ -40,7 +44,7 @@ func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, rpcCl
}) // respArr => (apiResp, error)
if !respArr[1].IsNil() { // rpcClient err != nil
err := respArr[1].Interface().(error)
tracelog.WriteErrorResponse(ctx, rpcFuncName, err, "callRpcResp", "error")
WriteErrorResponse(ctx, rpcFuncName, err, "callRpcResp", "error")
return
}
rpcResp := respArr[0].Elem()
@@ -48,7 +52,7 @@ func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, rpcCl
if apiResp != nil {
CopyAny(rpcResp.Interface(), apiResp)
}
tracelog.SetSuccess(ctx, rpcFuncName, apiResp)
SetSuccess(ctx, rpcFuncName, apiResp)
}
func rpcString(v interface{}) string {
@@ -57,3 +61,43 @@ func rpcString(v interface{}) string {
}
return fmt.Sprintf("%+v", v)
}
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{}) {
tracelog.SetCtxInfo(ctx, funcName, err, args)
e := tracelog.Unwrap(err)
switch t := e.(type) {
case *constant.ErrInfo:
ctx.Value(tracelog.TraceLogKey).(*tracelog.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(tracelog.TraceLogKey).(*tracelog.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(tracelog.TraceLogKey).(*tracelog.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 SetSuccess(ctx context.Context, funcName string, data interface{}) {
tracelog.SetCtxInfo(ctx, funcName, nil, "data", data)
ctx.Value(tracelog.TraceLogKey).(*tracelog.ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": "", "errDtl": "", "data": data})
}