improve handler response for ghttp.Server

This commit is contained in:
John Guo 2022-03-08 11:50:23 +08:00
parent 10ed04cdb8
commit 0b3cd7b7ae
3 changed files with 28 additions and 22 deletions

View File

@ -7,6 +7,8 @@
package ghttp
import (
"net/http"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/intlog"
@ -28,29 +30,33 @@ func MiddlewareHandlerResponse(r *Request) {
}
var (
ctx = r.Context()
err = r.GetError()
res = r.GetHandlerResponse()
internalErr error
msg string
ctx = r.Context()
err = r.GetError()
res = r.GetHandlerResponse()
code = gerror.Code(err)
)
if err != nil {
code := gerror.Code(err)
if code == gcode.CodeNil {
code = gcode.CodeInternalError
}
internalErr = r.Response.WriteJson(DefaultHandlerResponse{
Code: code.Code(),
Message: err.Error(),
Data: nil,
})
if internalErr != nil {
intlog.Errorf(ctx, `%+v`, internalErr)
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
}
return
} else {
code = gcode.CodeOK
}
internalErr = r.Response.WriteJson(DefaultHandlerResponse{
Code: gcode.CodeOK.Code(),
Message: "",
internalErr := r.Response.WriteJson(DefaultHandlerResponse{
Code: code.Code(),
Message: msg,
Data: res,
})
if internalErr != nil {

View File

@ -60,7 +60,7 @@ func Test_OpenApi_Swagger(t *testing.T) {
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/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, "/api.json"), `/test/error`), true)

View File

@ -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()))
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.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-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.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-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-test4?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Name":"john"}}`)