From 12e9febe9e63bb56a36fbbfd699971006aa6f43e Mon Sep 17 00:00:00 2001 From: John Guo Date: Wed, 22 Mar 2023 20:14:57 +0800 Subject: [PATCH] fix issue #2516 (#2531) * fix issue #2516 * golang ci configuration updates * add example for default value of http request --- .golangci.yml | 2 +- example/httpserver/default_value/main.go | 41 ++++++++++++++++++++++++ net/ghttp/ghttp_middleware_tracing.go | 10 ++++-- 3 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 example/httpserver/default_value/main.go diff --git a/.golangci.yml b/.golangci.yml index 4f062da0e9a..8ca32fa3299 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -135,7 +135,7 @@ linters-settings: goconst: # Minimal length of string constant. # Default: 3 - min-len: 2 + min-len: 4 # Minimum occurrences of constant string count to trigger issue. # Default: 3 # For subsequent optimization, the value is reduced. diff --git a/example/httpserver/default_value/main.go b/example/httpserver/default_value/main.go new file mode 100644 index 00000000000..dd6c6e105e9 --- /dev/null +++ b/example/httpserver/default_value/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "context" + + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/net/ghttp" +) + +type GetListReq struct { + g.Meta `path:"/" method:"get"` + Type string `v:"required#请选择内容模型" dc:"内容模型"` + Page int `v:"min:0#分页号码错误" dc:"分页号码" d:"1"` + Size int `v:"max:50#分页数量最大50条" dc:"分页数量,最大50" d:"10"` + Sort int `v:"in:0,1,2#排序类型不合法" dc:"排序类型(0:最新, 默认。1:活跃, 2:热度)"` +} +type GetListRes struct { + Items []Item `dc:"内容列表"` +} + +type Item struct { + Id int64 `dc:"内容ID"` + Title string `dc:"内容标题"` +} + +type Controller struct{} + +func (Controller) GetList(ctx context.Context, req *GetListReq) (res *GetListRes, err error) { + g.Log().Info(ctx, req) + return +} + +func main() { + s := g.Server() + s.Group("/content", func(group *ghttp.RouterGroup) { + group.Middleware(ghttp.MiddlewareHandlerResponse) + group.Bind(&Controller{}) + }) + s.SetPort(8199) + s.Run() +} diff --git a/net/ghttp/ghttp_middleware_tracing.go b/net/ghttp/ghttp_middleware_tracing.go index 84acf2c8ccc..2f8f438eca3 100644 --- a/net/ghttp/ghttp_middleware_tracing.go +++ b/net/ghttp/ghttp_middleware_tracing.go @@ -18,6 +18,7 @@ import ( "go.opentelemetry.io/otel/trace" "github.com/gogf/gf/v2" + "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/internal/httputil" "github.com/gogf/gf/v2/internal/utils" "github.com/gogf/gf/v2/net/gtrace" @@ -80,7 +81,12 @@ func internalMiddlewareServerTracing(r *Request) { } // Request content logging. - reqBodyContentBytes, _ := ioutil.ReadAll(r.Body) + reqBodyContentBytes, err := ioutil.ReadAll(r.Body) + if err != nil { + r.SetError(gerror.Wrap(err, `read request body failed`)) + span.SetStatus(codes.Error, fmt.Sprintf(`%+v`, err)) + return + } r.Body = utils.NewReadCloser(reqBodyContentBytes, false) span.AddEvent(tracingEventHttpRequest, trace.WithAttributes( @@ -97,7 +103,7 @@ func internalMiddlewareServerTracing(r *Request) { r.Middleware.Next() // Error logging. - if err := r.GetError(); err != nil { + if err = r.GetError(); err != nil { span.SetStatus(codes.Error, fmt.Sprintf(`%+v`, err)) } // Response content logging.