refactor: 错误消息脱敏 + 后端 CI
- 新增 utils.InternalMsg:内部错误详情只进服务端日志,对外统一脱敏 消息,不再向客户端泄露 GORM/SQL 实现细节;九个 controller 的 err.Error() 出口全部切换 - 新增 .github/workflows/ci.yml:go build/vet/test(backend,go 1.25)
This commit is contained in:
1 parent
1a47dee63d
commit
e0cef06234
11 files changed
+92
-49
No files matched your search
@@ -1,6 +1,8 @@
|
||||
package license
|
||||
|
||||
import (
|
||||
|
||||
"backend/utils"
|
||||
"backend/internal/dto"
|
||||
"net/http"
|
||||
|
||||
@@ -27,7 +29,7 @@ func (ctrl *LicenseController) ActivateDevice(c *gin.Context) {
|
||||
}
|
||||
data, err := ctrl.svc.Activate(req, c.ClientIP())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": utils.InternalMsg(err)})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "激活成功", "data": data})
|
||||
@@ -41,7 +43,7 @@ func (ctrl *LicenseController) Deactivate(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
if err := ctrl.svc.Deactivate(req); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": utils.InternalMsg(err)})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "停用成功"})
|
||||
@@ -56,7 +58,7 @@ func (ctrl *LicenseController) Heartbeat(c *gin.Context) {
|
||||
}
|
||||
data, err := ctrl.svc.Heartbeat(req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": utils.InternalMsg(err)})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "心跳成功", "data": data})
|
||||
@@ -73,7 +75,7 @@ func (ctrl *LicenseController) ListDevices(c *gin.Context) {
|
||||
}
|
||||
devices, maxDevices, err := ctrl.svc.ListDevicesByKey(req.LicenseKey)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": utils.InternalMsg(err)})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"code": 0, "data": gin.H{
|
||||
|
||||
@@ -80,7 +80,7 @@ func (ctrl *LicenseController) Create(c *gin.Context) {
|
||||
}
|
||||
licenses, err := ctrl.svc.Create(req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": utils.InternalMsg(err)})
|
||||
return
|
||||
}
|
||||
utils.LogAction(ctrl.db, c, "create", "license", licenses[0].ID, licenses[0].LicenseKey, "", "",
|
||||
@@ -97,7 +97,7 @@ func (ctrl *LicenseController) IssueOffline(c *gin.Context) {
|
||||
}
|
||||
result, err := ctrl.offline.Issue(req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": utils.InternalMsg(err)})
|
||||
return
|
||||
}
|
||||
utils.LogAction(ctrl.db, c, "create_offline", "license", result.LicenseID, result.Serial, "", "",
|
||||
@@ -128,7 +128,7 @@ func (ctrl *LicenseController) Update(c *gin.Context) {
|
||||
}
|
||||
lic, err := ctrl.svc.Update(uint(id), updates)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": utils.InternalMsg(err)})
|
||||
return
|
||||
}
|
||||
utils.LogAction(ctrl.db, c, "update", "license", lic.ID, lic.LicenseKey, "", "", "更新 License")
|
||||
@@ -140,7 +140,7 @@ func (ctrl *LicenseController) Delete(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
lic, err := ctrl.svc.Delete(uint(id))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": utils.InternalMsg(err)})
|
||||
return
|
||||
}
|
||||
utils.LogAction(ctrl.db, c, "delete", "license", lic.ID, lic.LicenseKey, "", "", "删除 License")
|
||||
@@ -156,7 +156,7 @@ func (ctrl *LicenseController) BatchDelete(c *gin.Context) {
|
||||
}
|
||||
result, err := ctrl.svc.BatchDelete(req.IDs)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": utils.InternalMsg(err)})
|
||||
return
|
||||
}
|
||||
utils.LogAction(ctrl.db, c, "batch_delete", "license", 0, "", "", "",
|
||||
@@ -185,7 +185,7 @@ func (ctrl *LicenseController) Renew(c *gin.Context) {
|
||||
}
|
||||
lic, err := ctrl.svc.Renew(uint(id), req.Duration)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": utils.InternalMsg(err)})
|
||||
return
|
||||
}
|
||||
utils.LogAction(ctrl.db, c, "renew", "license", lic.ID, lic.LicenseKey, "", "", "续期 License")
|
||||
@@ -201,7 +201,7 @@ func (ctrl *LicenseController) Revoke(c *gin.Context) {
|
||||
c.ShouldBindJSON(&req)
|
||||
lic, err := ctrl.svc.Revoke(uint(id), req.Reason)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": utils.InternalMsg(err)})
|
||||
return
|
||||
}
|
||||
utils.LogAction(ctrl.db, c, "revoke", "license", lic.ID, lic.LicenseKey, "", "", "吊销 License: "+req.Reason)
|
||||
@@ -223,7 +223,7 @@ func (ctrl *LicenseController) setStatus(c *gin.Context, status string) {
|
||||
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
lic, err := ctrl.svc.SetStatus(uint(id), status)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": utils.InternalMsg(err)})
|
||||
return
|
||||
}
|
||||
utils.LogAction(ctrl.db, c, status, "license", lic.ID, lic.LicenseKey, "", "", "License 状态变更为 "+status)
|
||||
@@ -235,7 +235,7 @@ func (ctrl *LicenseController) GetActivations(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
activations, err := ctrl.svc.GetActivations(uint(id))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": utils.InternalMsg(err)})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"code": 0, "data": activations})
|
||||
@@ -246,7 +246,7 @@ func (ctrl *LicenseController) ForceOffline(c *gin.Context) {
|
||||
licenseID, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
activationID, _ := strconv.ParseUint(c.Param("activationId"), 10, 32)
|
||||
if err := ctrl.svc.ForceOffline(uint(licenseID), uint(activationID)); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": utils.InternalMsg(err)})
|
||||
return
|
||||
}
|
||||
utils.LogAction(ctrl.db, c, "force_offline", "activation", uint(activationID), "", "", "", "强制下线设备")
|
||||
@@ -257,7 +257,7 @@ func (ctrl *LicenseController) ForceOffline(c *gin.Context) {
|
||||
func (ctrl *LicenseController) Export(c *gin.Context) {
|
||||
csv, err := ctrl.svc.ExportCSV(parseListQuery(c))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
||||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": utils.InternalMsg(err)})
|
||||
return
|
||||
}
|
||||
c.Header("Content-Type", "text/csv; charset=utf-8")
|
||||
|
||||
Reference in new issue
Block a user