gin icon indicating copy to clipboard operation
gin copied to clipboard

*gin.Context.JSON http: panic serving 10.13.207.251:25692: %!v(PANIC=Error method: runtime error: invalid memory address or nil pointer dereference) goroutine 1547630481 [running]: net/http.(*conn).serve.func1()

Open Yii18 opened this issue 2 years ago • 1 comments

http: panic serving 10.13.207.251:25692: %!v(PANIC=Error method: runtime error: invalid memory address or nil pointer dereference) goroutine 1547630481 [running]: net/http.(*conn).serve.func1()

func OK(c *gin.Context, data interface{}, msg ...string) { message := "" if len(msg) != 0 { message = msg[0] } else { message = "OK" } c.JSON(http.StatusOK, gin.H{"code": 0, "data": data, "message": message}) }

{ "level":"ERROR", "time":"2022-08-28T04:14:48.598Z", "caller":"logger/logger.go:127", "msg":"[Recovery from panic]", "errorError":"PANIC=runtime error: invalid memory address or nil pointer dereference", "request":"", "stack":"goroutine 801180736 [running]:\nruntime/debug.Stack()\n\t/usr/local/go/src/runtime/debug/stack.go:24 +0x65\ndemo/pkg/logger.GinRecovery.func1(0xc001a04d00, {0xf0e480, 0xc000987560})\n\t/home/jenkins/agent/workspace/golang-standardization-global-v2.0/pkg/logger/logger.go:126 +0x6e\ngithub.com/gin-gonic/gin.CustomRecoveryWithWriter.func1.1()\n\t/go/pkg/mod/github.com/gin-gonic/[email protected]/recovery.go:95 +0x38a\npanic({0xf0e480, 0xc000987560})\n\t/usr/local/go/src/runtime/panic.go:838 +0x207\nencoding/json.(*encodeState).marshal.func1()\n\t/usr/local/go/src/encoding/json/encode.go:328 +0x6e\npanic({0xf0e480, 0xc000987560})\n\t/usr/local/go/src/runtime/panic.go:838 +0x207\nencoding/json.typeEncoder({0x1241fc0?, 0xf26880})\n\t/usr/local/go/src/encoding/json/encode.go:383 +0x205\nencoding/json.valueEncoder({0xf26880?, 0xc000987530?, 0xc000e5ae78?})\n\t/usr/local/go/src/encoding/json/encode.go:378 +0x2f\nencoding/json.(*encodeState).reflectValue(0xc000e5ae88?, {0xf26880?, 0xc000987530?, 0x0?}, {0xe0?, 0x50?})\n\t/usr/local/go/src/encoding/json/encode.go:360 +0x4a\nencoding/json.(*encodeState).marshal(0xc000e5aee0?, {0xf26880?, 0xc000987530?}, {0x18?, 0xaf?})\n\t/usr/local/go/src/encoding/json/encode.go:332 +0xfa\nencoding/json.Marshal({0xf26880, 0xc000987530})\n\t/usr/local/go/src/encoding/json/encode.go:161 +0x45\ngithub.com/gin-gonic/gin/render.WriteJSON({0x7f5328c689e0, 0xc0019faa50}, {0xf26880, 0xc000987530})\n\t/go/pkg/mod/github.com/gin-gonic/[email protected]/render/json.go:69 +0x63\ngithub.com/gin-gonic/gin/render.JSON.Render(...)\n\t/go/pkg/mod/github.com/gin-gonic/[email protected]/render/json.go:55\ngithub.com/gin-gonic/gin.(*Context).Render(0xc001a04d00, 0xc8, {0x12369b8, 0xc0026ad9c0})\n\t/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:913 +0xf8\ngithub.com/gin-gonic/gin.(*Context).JSON(...)\n\t/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:956\ndemo/internal/response.OK(0x18f5230?, {0xea9f60?, 0xc001e15ef0?}, {0x0?, 0x4?, 0x2adfe8bac?})\n\t/home/jenkins/agent/workspace/golang-standardization-global-v2.0/internal/response/response.go:41 +0x22d\ndemo/cmd/demo/router/handler.(*RecommendHandler).GetUserFeatureResult(0x203000?, 0xc001a04d00)\n\t/home/jenkins/agent/workspace/golang-standardization-global-v2.0/cmd/demo/router/handler/recommend_handler.go:78 +0xd0\ngithub.com/gin-gonic/gin.(*Context).Next(...)\n\t/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:165\ndemo/pkg/auth.Auth.func1(0xc001a04d00)\n\t/home/jenkins/agent/workspace/golang-standardization-global-v2.0/pkg/auth/auth.go:34 +0x2c7\ngithub.com/gin-gonic/gin.(*Context).Next(...)\n\t/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:165\ngithub.com/gin-gonic/gin.CustomRecoveryWithWriter.func1(0xc001a04d00)\n\t/go/pkg/mod/github.com/gin-gonic/[email protected]/recovery.go:99 +0x82\ngithub.com/gin-gonic/gin.(*Context).Next(...)\n\t/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:165\ndemo/pkg/logger.AddTraceId.func1(0xc001a04d00)\n\t/home/jenkins/agent/workspace/golang-standardization-global-v2.0/pkg/logger/logger.go:153 +0x4be\ngithub.com/gin-gonic/gin.(*Context).Next(...)\n\t/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:165\ndemo/pkg/middleware.CatchRequestAndResponse.func1(0xc001a04d00)\n\t/home/jenkins/agent/workspace/golang-standardization-global-v2.0/pkg/middleware/request_response.go:36 +0x2f6\ngithub.com/gin-gonic/gin.(*Context).Next(...)\n\t/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:165\ndemo/pkg/logger.GinLogger.func1(0xc001a04d00)\n\t/home/jenkins/agent/workspace/golang-standardization-global-v2.0/pkg/logger/logger.go:76 +0xa6\ngithub.com/gin-gonic/gin.(*Context).Next(...)\n\t/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:165\ngithub.com/gin-gonic/gin.(*Engine).handleHTTPRequest(0xc0005836c0, 0xc001a04d00)\n\t/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:489 +0x63e\ngithub.com/gin-gonic/gin.(*Engine).ServeHTTP(0xc0005836c0, {0x1237990?, 0xc0019ad6c0}, 0xc000e04700)\n\t/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:445 +0x1c5\nnet/http.serverHandler.ServeHTTP({0x1235f68?}, {0x1237990, 0xc0019ad6c0}, 0xc000e04700)\n\t/usr/local/go/src/net/http/server.go:2916 +0x43b\nnet/http.(*conn).serve(0xc0014b54a0, {0x1238640, 0xc0006681e0})\n\t/usr/local/go/src/net/http/server.go:1966 +0x5d7\ncreated by net/http.(*Server).Serve\n\t/usr/local/go/src/net/http/server.go:3071 +0x4db\n" }

Yii18 avatar Aug 28 '22 13:08 Yii18

according to docs/Readme you have to use c.Param

func OK(c *gin.Context) {
message := ""
//msg := []string{}
//data := interface{}
// or
msg := c.Param("msg")
data := c.Param("data")

if msg != "" { message = msg } else { message = "OK" }
c.JSON(http.StatusOK, gin.H{"code": 0, "data": data, "message": message}) }

Gasoid avatar Sep 05 '22 07:09 Gasoid