Skip to content

Commit 41539a8

Browse files
committed
chore: add more info for panic fix
1 parent 751b7ce commit 41539a8

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

middleware/recover.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package middleware
33
import (
44
"fmt"
55
"github.com/gin-gonic/gin"
6+
"github.com/songquanpeng/one-api/common"
67
"github.com/songquanpeng/one-api/common/logger"
78
"net/http"
89
"runtime/debug"
@@ -12,11 +13,15 @@ func RelayPanicRecover() gin.HandlerFunc {
1213
return func(c *gin.Context) {
1314
defer func() {
1415
if err := recover(); err != nil {
15-
logger.SysError(fmt.Sprintf("panic detected: %v", err))
16-
logger.SysError(fmt.Sprintf("stacktrace from panic: %s", string(debug.Stack())))
16+
ctx := c.Request.Context()
17+
logger.Errorf(ctx, fmt.Sprintf("panic detected: %v", err))
18+
logger.Errorf(ctx, fmt.Sprintf("stacktrace from panic: %s", string(debug.Stack())))
19+
logger.Errorf(ctx, fmt.Sprintf("request: %s %s", c.Request.Method, c.Request.URL.Path))
20+
body, _ := common.GetRequestBody(c)
21+
logger.Errorf(ctx, fmt.Sprintf("request body: %s", string(body)))
1722
c.JSON(http.StatusInternalServerError, gin.H{
1823
"error": gin.H{
19-
"message": fmt.Sprintf("Panic detected, error: %v. Please submit a issue here: https://github.com/songquanpeng/one-api", err),
24+
"message": fmt.Sprintf("Panic detected, error: %v. Please submit an issue with the related log here: https://github.com/songquanpeng/one-api", err),
2025
"type": "one_api_panic",
2126
},
2227
})

0 commit comments

Comments
 (0)