From 09f83bdd58c36d6c73c8c367d90e6fa9471da5c7 Mon Sep 17 00:00:00 2001 From: John Guo Date: Thu, 6 Jan 2022 21:47:36 +0800 Subject: [PATCH] add panic for ghttp.Server.GetBody --- net/ghttp/ghttp_request_param.go | 9 ++++++--- net/ghttp/ghttp_z_unit_feature_config_test.go | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/net/ghttp/ghttp_request_param.go b/net/ghttp/ghttp_request_param.go index c6afe25e0..7f02ea489 100644 --- a/net/ghttp/ghttp_request_param.go +++ b/net/ghttp/ghttp_request_param.go @@ -124,7 +124,6 @@ func (r *Request) doParse(pointer interface{}, requestType int) error { return err } for i := 0; i < reflectVal2.Len(); i++ { - if err = gvalid.New(). Data(reflectVal2.Index(i)).Assoc(j.Get(gconv.String(i)).Map()). Run(r.Context()); err != nil { @@ -146,7 +145,11 @@ func (r *Request) Get(key string, def ...interface{}) *gvar.Var { // It can be called multiple times retrieving the same body content. func (r *Request) GetBody() []byte { if r.bodyContent == nil { - r.bodyContent, _ = ioutil.ReadAll(r.Body) + var err error + r.bodyContent, err = ioutil.ReadAll(r.Body) + if err != nil { + panic(gerror.WrapCode(gcode.CodeInternalError, err, `ReadAll from body failed`)) + } r.Body = utils.NewReadCloser(r.bodyContent, true) } return r.bodyContent @@ -192,7 +195,7 @@ func (r *Request) parseQuery() { var err error r.queryMap, err = gstr.Parse(r.URL.RawQuery) if err != nil { - panic(gerror.WrapCode(gcode.CodeInvalidParameter, err, "")) + panic(gerror.WrapCode(gcode.CodeInvalidParameter, err, "Parse Query failed")) } } } diff --git a/net/ghttp/ghttp_z_unit_feature_config_test.go b/net/ghttp/ghttp_z_unit_feature_config_test.go index 593b6d21f..d0851c809 100644 --- a/net/ghttp/ghttp_z_unit_feature_config_test.go +++ b/net/ghttp/ghttp_z_unit_feature_config_test.go @@ -91,7 +91,7 @@ func Test_ClientMaxBodySize(t *testing.T) { } t.Assert( gstr.Trim(c.PostContent(ctx, "/", data)), - data[:1024], + `ReadAll from body failed: http: request body too large`, ) }) }