diff --git a/pkg/common/constant/error.go b/pkg/common/constant/error.go index e30f3ca87..d9c4b48b7 100644 --- a/pkg/common/constant/error.go +++ b/pkg/common/constant/error.go @@ -34,6 +34,8 @@ var ( ErrOwnerNotAllowedQuit = ErrInfo{OwnerNotAllowedQuitError, "OwnerNotAllowedQuitError"} ErrRegisteredAlready = ErrInfo{RegisteredAlreadyError, "RegisteredAlreadyError"} + ErrDefaultOther = ErrInfo{DefaultOtherError, "DefaultOtherError"} + ErrTokenExpired = ErrInfo{TokenExpiredError, "TokenExpiredError"} ErrTokenInvalid = ErrInfo{TokenInvalidError, "TokenInvalidError"} // ErrTokenMalformed = ErrInfo{TokenMalformedError, "TokenMalformedError"} //格式错误 @@ -70,6 +72,8 @@ const ( ServerInternalError = 90003 //服务器内部错误 NetworkError = 90004 //网络错误 NoPermissionError = 90005 //权限不足 + + DefaultOtherError = 90006 //其他错误 ) // 账号错误码 diff --git a/pkg/common/trace_log/ctx.go b/pkg/common/trace_log/ctx.go index 09cce52a3..e80905839 100644 --- a/pkg/common/trace_log/ctx.go +++ b/pkg/common/trace_log/ctx.go @@ -7,21 +7,15 @@ import ( "errors" "fmt" "github.com/gin-gonic/gin" + "net/http" ) const TraceLogKey = "trace_log" const GinContextKey = "gin_context" -func ToErrInfoWithErr(errCode int32, errMsg string) *ErrInfo { - return nil -} - -func getAPIErrorResponse(c *gin.Context, errInfo ErrInfo) { - -} - func NewCtx(c *gin.Context, api string) context.Context { - req := ApiInfo{ApiName: api} + req := ApiInfo{ApiName: api, GinCtx: c} + req.OperationID = c.PostForm("operationID") return context.WithValue(c, GinContextKey, req) } @@ -32,38 +26,45 @@ func ShowLog(ctx context.Context) { if v.Err != nil { log.Error(v.FuncName, v.Err, v.Args) } else { - log.Info(v.FuncName, v.Err, v.Args) + log.Info(v.FuncName, v.Args) } } - } -func WriteErrorResponse(c *gin.Context, err error) { +func WriteErrorResponse(ctx context.Context, funcName string, err error) { + SetContextInfo(ctx, funcName, err) e := new(constant.ErrInfo) - if errors.As(err, &e) { - + switch { + case errors.As(err, &e): + ctx.Value(GinContextKey).(ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": e.ErrCode, "errMsg": e.ErrMsg}) + return + default: + ctx.Value(GinContextKey).(ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": constant.ErrDefaultOther.ErrCode, "errMsg": constant.ErrDefaultOther.ErrMsg}) + return } } -type FuncInfo struct { - FuncName string - Args map[string]interface{} - Err error -} - type ApiInfo struct { ApiName string OperationID string Funcs []FuncInfo + GinCtx *gin.Context +} + +type FuncInfo struct { + FuncName string + Args map[string]interface{} + Err error } func SetContextInfo(ctx context.Context, funcName string, err error, args ...interface{}) { - var req ReqInfo - t := ctx.Value("f").([]ReqInfo) - argsHandle(args, req.Args) - req.FuncName = funcName - req.Err = err - t = append(t, req) + t := ctx.Value(TraceLogKey).(ApiInfo) + var funcInfo FuncInfo + funcInfo.Args = make(map[string]interface{}) + argsHandle(args, funcInfo.Args) + funcInfo.FuncName = funcName + funcInfo.Err = err + t.Funcs = append(t.Funcs, funcInfo) } func argsHandle(args []interface{}, fields map[string]interface{}) {