mirror of
https://gitee.com/johng/gf.git
synced 2024-12-04 05:07:44 +08:00
improve handler response for ghttp.Server
This commit is contained in:
parent
10ed04cdb8
commit
0b3cd7b7ae
@ -7,6 +7,8 @@
|
|||||||
package ghttp
|
package ghttp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/gogf/gf/v2/errors/gcode"
|
"github.com/gogf/gf/v2/errors/gcode"
|
||||||
"github.com/gogf/gf/v2/errors/gerror"
|
"github.com/gogf/gf/v2/errors/gerror"
|
||||||
"github.com/gogf/gf/v2/internal/intlog"
|
"github.com/gogf/gf/v2/internal/intlog"
|
||||||
@ -28,29 +30,33 @@ func MiddlewareHandlerResponse(r *Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
msg string
|
||||||
ctx = r.Context()
|
ctx = r.Context()
|
||||||
err = r.GetError()
|
err = r.GetError()
|
||||||
res = r.GetHandlerResponse()
|
res = r.GetHandlerResponse()
|
||||||
internalErr error
|
code = gerror.Code(err)
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
code := gerror.Code(err)
|
|
||||||
if code == gcode.CodeNil {
|
if code == gcode.CodeNil {
|
||||||
code = gcode.CodeInternalError
|
code = gcode.CodeInternalError
|
||||||
}
|
}
|
||||||
internalErr = r.Response.WriteJson(DefaultHandlerResponse{
|
msg = err.Error()
|
||||||
|
} else if r.Response.Status > 0 && r.Response.Status != http.StatusOK {
|
||||||
|
msg = http.StatusText(r.Response.Status)
|
||||||
|
switch r.Response.Status {
|
||||||
|
case http.StatusNotFound:
|
||||||
|
code = gcode.CodeNotFound
|
||||||
|
case http.StatusForbidden:
|
||||||
|
code = gcode.CodeNotAuthorized
|
||||||
|
default:
|
||||||
|
code = gcode.CodeUnknown
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
code = gcode.CodeOK
|
||||||
|
}
|
||||||
|
internalErr := r.Response.WriteJson(DefaultHandlerResponse{
|
||||||
Code: code.Code(),
|
Code: code.Code(),
|
||||||
Message: err.Error(),
|
Message: msg,
|
||||||
Data: nil,
|
|
||||||
})
|
|
||||||
if internalErr != nil {
|
|
||||||
intlog.Errorf(ctx, `%+v`, internalErr)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
internalErr = r.Response.WriteJson(DefaultHandlerResponse{
|
|
||||||
Code: gcode.CodeOK.Code(),
|
|
||||||
Message: "",
|
|
||||||
Data: res,
|
Data: res,
|
||||||
})
|
})
|
||||||
if internalErr != nil {
|
if internalErr != nil {
|
||||||
|
@ -60,7 +60,7 @@ func Test_OpenApi_Swagger(t *testing.T) {
|
|||||||
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
||||||
|
|
||||||
t.Assert(c.GetContent(ctx, "/test?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Age":18,"Name":"john"}}`)
|
t.Assert(c.GetContent(ctx, "/test?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Age":18,"Name":"john"}}`)
|
||||||
t.Assert(c.GetContent(ctx, "/test/error"), `{"code":50,"message":"error","data":null}`)
|
t.Assert(c.GetContent(ctx, "/test/error"), `{"code":50,"message":"error","data":{"Id":1,"Age":0,"Name":""}}`)
|
||||||
|
|
||||||
t.Assert(gstr.Contains(c.GetContent(ctx, "/swagger/"), `API Reference`), true)
|
t.Assert(gstr.Contains(c.GetContent(ctx, "/swagger/"), `API Reference`), true)
|
||||||
t.Assert(gstr.Contains(c.GetContent(ctx, "/api.json"), `/test/error`), true)
|
t.Assert(gstr.Contains(c.GetContent(ctx, "/api.json"), `/test/error`), true)
|
||||||
|
@ -56,7 +56,7 @@ func Test_Router_Handler_Extended_Handler_WithObject(t *testing.T) {
|
|||||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
||||||
|
|
||||||
t.Assert(client.GetContent(ctx, "/test?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Age":18,"Name":"john"}}`)
|
t.Assert(client.GetContent(ctx, "/test?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Age":18,"Name":"john"}}`)
|
||||||
t.Assert(client.GetContent(ctx, "/test/error"), `{"code":50,"message":"error","data":null}`)
|
t.Assert(client.GetContent(ctx, "/test/error"), `{"code":50,"message":"error","data":{"Id":1,"Age":0,"Name":""}}`)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,10 +146,10 @@ func Test_Router_Handler_Extended_Handler_WithObjectAndMeta(t *testing.T) {
|
|||||||
client := g.Client()
|
client := g.Client()
|
||||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
||||||
|
|
||||||
t.Assert(client.GetContent(ctx, "/"), `{"code":0,"message":"","data":null}`)
|
t.Assert(client.GetContent(ctx, "/"), `{"code":65,"message":"Not Found","data":null}`)
|
||||||
t.Assert(client.GetContent(ctx, "/custom-test1?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Age":18}}`)
|
t.Assert(client.GetContent(ctx, "/custom-test1?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Age":18}}`)
|
||||||
t.Assert(client.GetContent(ctx, "/custom-test2?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Name":"john"}}`)
|
t.Assert(client.GetContent(ctx, "/custom-test2?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Name":"john"}}`)
|
||||||
t.Assert(client.PostContent(ctx, "/custom-test2?age=18&name=john"), `{"code":0,"message":"","data":null}`)
|
t.Assert(client.PostContent(ctx, "/custom-test2?age=18&name=john"), `{"code":65,"message":"Not Found","data":null}`)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,10 +177,10 @@ func Test_Router_Handler_Extended_Handler_Group_Bind(t *testing.T) {
|
|||||||
client := g.Client()
|
client := g.Client()
|
||||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
||||||
|
|
||||||
t.Assert(client.GetContent(ctx, "/"), `{"code":0,"message":"","data":null}`)
|
t.Assert(client.GetContent(ctx, "/"), `{"code":65,"message":"Not Found","data":null}`)
|
||||||
t.Assert(client.GetContent(ctx, "/api/v1/custom-test1?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Age":18}}`)
|
t.Assert(client.GetContent(ctx, "/api/v1/custom-test1?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Age":18}}`)
|
||||||
t.Assert(client.GetContent(ctx, "/api/v1/custom-test2?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Name":"john"}}`)
|
t.Assert(client.GetContent(ctx, "/api/v1/custom-test2?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Name":"john"}}`)
|
||||||
t.Assert(client.PostContent(ctx, "/api/v1/custom-test2?age=18&name=john"), `{"code":0,"message":"","data":null}`)
|
t.Assert(client.PostContent(ctx, "/api/v1/custom-test2?age=18&name=john"), `{"code":65,"message":"Not Found","data":null}`)
|
||||||
|
|
||||||
t.Assert(client.GetContent(ctx, "/api/v1/custom-test3?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Age":18}}`)
|
t.Assert(client.GetContent(ctx, "/api/v1/custom-test3?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Age":18}}`)
|
||||||
t.Assert(client.GetContent(ctx, "/api/v1/custom-test4?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Name":"john"}}`)
|
t.Assert(client.GetContent(ctx, "/api/v1/custom-test4?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Name":"john"}}`)
|
||||||
|
Loading…
Reference in New Issue
Block a user