捕获runtime.Error异常,否则接口报错不返回任何信息

报错细节不应该隐藏,方便debug。500错误应该由前端统一处理,返回用户可读信息。
This commit is contained in:
zhaoyidong
2022-08-18 10:02:00 +08:00
committed by GitHub
parent 66c8eb5ee8
commit 8df2e8190e
+7 -1
View File
@@ -3,6 +3,7 @@ package middleware
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"runtime"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -43,9 +44,14 @@ func CustomError(c *gin.Context) {
} else { } else {
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"code": 500, "code": 500,
"msg": "未知异常,请联系管理员!", "msg": errStr,
}) })
} }
case runtime.Error:
c.JSON(http.StatusOK, gin.H{
"code": 500,
"msg": errStr.Error(),
})
default: default:
panic(err) panic(err)
} }