From 8df2e8190e54812fb21c6be76c84fc53b5690799 Mon Sep 17 00:00:00 2001 From: zhaoyidong Date: Thu, 18 Aug 2022 10:02:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8D=95=E8=8E=B7runtime.Error=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=EF=BC=8C=E5=90=A6=E5=88=99=E6=8E=A5=E5=8F=A3=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E4=B8=8D=E8=BF=94=E5=9B=9E=E4=BB=BB=E4=BD=95=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 报错细节不应该隐藏,方便debug。500错误应该由前端统一处理,返回用户可读信息。 --- common/middleware/customerror.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/common/middleware/customerror.go b/common/middleware/customerror.go index 121a2e7..1f92628 100644 --- a/common/middleware/customerror.go +++ b/common/middleware/customerror.go @@ -3,6 +3,7 @@ package middleware import ( "fmt" "net/http" + "runtime" "strconv" "strings" "time" @@ -43,9 +44,14 @@ func CustomError(c *gin.Context) { } else { c.JSON(http.StatusOK, gin.H{ "code": 500, - "msg": "未知异常,请联系管理员!", + "msg": errStr, }) } + case runtime.Error: + c.JSON(http.StatusOK, gin.H{ + "code": 500, + "msg": errStr.Error(), + }) default: panic(err) }