mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
fix issue #1626
This commit is contained in:
@ -28,12 +28,11 @@ func MiddlewareHandlerResponse(r *Request) {
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
res interface{}
|
||||
ctx = r.Context()
|
||||
err = r.GetError()
|
||||
res = r.GetHandlerResponse()
|
||||
internalErr error
|
||||
)
|
||||
res, err = r.GetHandlerResponse()
|
||||
if err != nil {
|
||||
code := gerror.Code(err)
|
||||
if code == gcode.CodeNil {
|
||||
|
||||
@ -41,7 +41,7 @@ type Request struct {
|
||||
|
||||
context context.Context // Custom context for internal usage purpose.
|
||||
handlers []*handlerParsedItem // All matched handlers containing handler, hook and middleware for this request.
|
||||
handlerResponse handlerResponse // Handler response object and its error value for Request/Response handler.
|
||||
handlerResponse interface{} // Handler response object for Request/Response handler.
|
||||
hasHookHandler bool // A bool marking whether there's hook handler in the handlers for performance purpose.
|
||||
hasServeHandler bool // A bool marking whether there's serving handler in the handlers for performance purpose.
|
||||
parsedQuery bool // A bool marking whether the GET parameters parsed.
|
||||
@ -267,6 +267,6 @@ func (r *Request) ReloadParam() {
|
||||
}
|
||||
|
||||
// GetHandlerResponse retrieves and returns the handler response object and its error.
|
||||
func (r *Request) GetHandlerResponse() (res interface{}, err error) {
|
||||
return r.handlerResponse.Object, r.handlerResponse.Error
|
||||
func (r *Request) GetHandlerResponse() interface{} {
|
||||
return r.handlerResponse
|
||||
}
|
||||
|
||||
@ -138,12 +138,12 @@ func (m *middleware) callHandlerFunc(funcInfo handlerFuncInfo) {
|
||||
)
|
||||
if funcInfo.Type.In(1).Kind() == reflect.Ptr {
|
||||
inputObject = reflect.New(funcInfo.Type.In(1).Elem())
|
||||
m.request.handlerResponse.Error = m.request.Parse(inputObject.Interface())
|
||||
m.request.error = m.request.Parse(inputObject.Interface())
|
||||
} else {
|
||||
inputObject = reflect.New(funcInfo.Type.In(1).Elem()).Elem()
|
||||
m.request.handlerResponse.Error = m.request.Parse(inputObject.Addr().Interface())
|
||||
m.request.error = m.request.Parse(inputObject.Addr().Interface())
|
||||
}
|
||||
if m.request.handlerResponse.Error != nil {
|
||||
if m.request.error != nil {
|
||||
return
|
||||
}
|
||||
inputValues = append(inputValues, inputObject)
|
||||
@ -155,15 +155,15 @@ func (m *middleware) callHandlerFunc(funcInfo handlerFuncInfo) {
|
||||
case 1:
|
||||
if !results[0].IsNil() {
|
||||
if err, ok := results[0].Interface().(error); ok {
|
||||
m.request.handlerResponse.Error = err
|
||||
m.request.error = err
|
||||
}
|
||||
}
|
||||
|
||||
case 2:
|
||||
m.request.handlerResponse.Object = results[0].Interface()
|
||||
m.request.handlerResponse = results[0].Interface()
|
||||
if !results[1].IsNil() {
|
||||
if err, ok := results[1].Interface().(error); ok {
|
||||
m.request.handlerResponse.Error = err
|
||||
m.request.error = err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,6 +16,7 @@ import (
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/guid"
|
||||
)
|
||||
|
||||
@ -190,3 +191,42 @@ func Test_Router_Handler_Extended_Handler_Group_Bind(t *testing.T) {
|
||||
t.Assert(client.GetContent(ctx, "/api/v2/custom-test4?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Name":"john"}}`)
|
||||
})
|
||||
}
|
||||
|
||||
// https://github.com/gogf/gf/issues/1626
|
||||
func Test_Issue1626(t *testing.T) {
|
||||
type TestReq struct {
|
||||
Name string `v:"required"`
|
||||
}
|
||||
type TestRes struct {
|
||||
Name string
|
||||
}
|
||||
s := g.Server(guid.S())
|
||||
s.Use(
|
||||
ghttp.MiddlewareHandlerResponse,
|
||||
func(r *ghttp.Request) {
|
||||
r.Middleware.Next()
|
||||
if err := r.GetError(); err != nil {
|
||||
r.Response.ClearBuffer()
|
||||
r.Response.Write(err.Error())
|
||||
}
|
||||
},
|
||||
)
|
||||
s.BindHandler("/test", func(ctx context.Context, req *TestReq) (res *TestRes, err error) {
|
||||
return &TestRes{Name: req.Name}, nil
|
||||
})
|
||||
s.SetDumpRouterMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
c := g.Client()
|
||||
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
||||
|
||||
t.Assert(c.GetContent(ctx, "/test"), `The Name field is required`)
|
||||
t.Assert(
|
||||
gstr.Contains(c.GetContent(ctx, "/test?name=john"), `{"Name":"john"}`),
|
||||
true,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user