提交 e9fe6fb4 authored 作者: mooncake9527's avatar mooncake9527

err notice

上级 abd8b6e1
......@@ -39,7 +39,7 @@ func defaultOptions() *options {
maxLength: defaultMaxLength,
log: defaultLogger,
ignoreRoutes: defaultIgnoreRoutes,
requestIDFrom: 0,
traceIDFrom: 0,
}
}
......@@ -47,7 +47,7 @@ type options struct {
maxLength int
log *zap.Logger
ignoreRoutes map[string]struct{}
requestIDFrom int // 0: ignore, 1: from context, 2: from header
traceIDFrom int // 0: ignore, 1: from context, 2: from header
}
func (o *options) apply(opts ...Option) {
......@@ -84,17 +84,17 @@ func WithIgnoreRoutes(routes ...string) Option {
}
}
// WithRequestIDFromContext name is field in context, default value is request_id
func WithRequestIDFromContext() Option {
// WithTraceIDFromContext name is field in context, default value is request_id
func WithTraceIDFromContext() Option {
return func(o *options) {
o.requestIDFrom = 1
o.traceIDFrom = 1
}
}
// WithRequestIDFromHeader name is field in header, default value is X-Request-Id
func WithRequestIDFromHeader() Option {
// WithTraceIDFromHeader name is field in header, default value is X-Request-Id
func WithTraceIDFromHeader() Option {
return func(o *options) {
o.requestIDFrom = 2
o.traceIDFrom = 2
}
}
......@@ -174,14 +174,14 @@ func Logging(opts ...Option) gin.HandlerFunc {
}
reqID := ""
if o.requestIDFrom == 1 {
if o.traceIDFrom == 1 {
if v, isExist := c.Get(ctxUtil.ContextTraceIDKey); isExist {
if requestID, ok := v.(string); ok {
reqID = requestID
fields = append(fields, zap.String(ctxUtil.ContextTraceIDKey, reqID))
}
}
} else if o.requestIDFrom == 2 {
} else if o.traceIDFrom == 2 {
reqID = c.Request.Header.Get(ctxUtil.HeaderXRequestIDKey)
fields = append(fields, zap.String(ctxUtil.ContextTraceIDKey, reqID))
}
......@@ -232,13 +232,13 @@ func SimpleLog(opts ...Option) gin.HandlerFunc {
}
reqID := ""
if o.requestIDFrom == 1 {
if o.traceIDFrom == 1 {
if v, isExist := c.Get(ctxUtil.ContextTraceIDKey); isExist {
if requestID, ok := v.(string); ok {
reqID = requestID
}
}
} else if o.requestIDFrom == 2 {
} else if o.traceIDFrom == 2 {
reqID = c.Request.Header.Get(ctxUtil.HeaderXRequestIDKey)
}
......
......@@ -32,8 +32,8 @@ func runLogHTTPServer() string {
r.Use(Logging(
WithLog(logger.Get()),
WithMaxLen(40),
WithRequestIDFromHeader(),
WithRequestIDFromContext(),
WithTraceIDFromHeader(),
WithTraceIDFromContext(),
WithIgnoreRoutes("/ping"), // ignore path /ping
))
......@@ -162,8 +162,8 @@ func runLogHTTPServer2() string {
r.Use(SimpleLog(
WithLog(logger.Get()),
WithMaxLen(200),
WithRequestIDFromContext(),
WithRequestIDFromHeader(),
WithTraceIDFromContext(),
WithTraceIDFromHeader(),
))
pingFun := func(c *gin.Context) {
......
......@@ -123,6 +123,15 @@ func WrapCtx(c *gin.Context) context.Context {
return ctx
}
func WrapAsyncCtx(c *gin.Context) context.Context {
ctx := context.WithValue(context.Background(), ctxUtil.ContextTraceIDKey, c.GetString(ctxUtil.ContextTraceIDKey)) //nolint
for k, v := range c.Keys {
ctx = context.WithValue(ctx, k, v) //nolint
}
ctx = context.WithValue(ctx, ctxUtil.GinContextKey, c) //nolint
return ctx
}
// AdaptCtx adapt context, if ctx is gin.Context, return gin.Context and context of the transformation
func AdaptCtx(ctx context.Context) (*gin.Context, context.Context) {
c, ok := ctx.(*gin.Context)
......
......@@ -59,6 +59,10 @@ func writeContentType(w http.ResponseWriter, value []string) {
func writeJSON(c *gin.Context, code int, res interface{}) {
c.Writer.WriteHeader(code)
writeContentType(c.Writer, jsonContentType)
//if res != nil {
// resBytes, _ := json.Marshal(res)
// c.Set(xctx.KeyResp, resBytes)
//}
err := json.NewEncoder(c.Writer).Encode(res)
if err != nil {
fmt.Printf("json encode error, err = %s\n", err.Error())
......
......@@ -32,6 +32,8 @@ var (
KeyUName = "uname"
KeyToken = "token"
KeyUser = "user"
KeyResp = "resp"
)
var (
......@@ -202,3 +204,14 @@ func getCtxVal[T any](c context.Context, key string) T {
var t T
return t
}
func GetResp(c *gin.Context) []byte {
v, ok1 := c.Get(KeyResp)
if ok1 {
ret, ok2 := v.([]byte)
if ok2 {
return ret
}
}
return nil
}
......@@ -2,6 +2,7 @@ package xslice
import (
"fmt"
"gitlab.wanzhuangkj.com/tush/xpkg/errcode"
"gitlab.wanzhuangkj.com/tush/xpkg/xtype"
"strings"
......@@ -230,19 +231,17 @@ func GetIDMapSliceIDs[ID xtype.Key, T IID[ID]](m map[ID][]*T) []ID {
return st.ToList()
}
func CompareSlice[ID xtype.Key, T IIDTable[ID], BizSlice ~[]*T](ids []ID, bizSlice BizSlice, errCode int) error {
func CompareSlice[ID xtype.Key, T IIDTable[ID], BizSlice ~[]*T](ids []ID, bizSlice BizSlice, errCode *errcode.Error) error {
if len(ids) == 0 {
return nil
}
if len(bizSlice) < len(ids) {
if len(bizSlice) == 0 {
var t T
return xerror.NewC(errCode, fmt.Sprintf("未查询到%s", t.TableName()))
return xerror.NewC(errCode.Code(), errCode.Msg())
}
deltaIDs := SetDifference(ids, GetIDs[ID](bizSlice))
if len(deltaIDs) > 0 {
var t T
return xerror.NewC(errCode, fmt.Sprintf("未查询到%s[ids:%s]", t.TableName(), Join(deltaIDs, ",")))
return xerror.NewC(errCode.Code(), fmt.Sprintf("%s[ids:%s]", errCode.Msg(), Join(deltaIDs, ",")))
}
}
return nil
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论