feat(quality): Q5 后端错误处理治理扩展 — RDP/API Token/AIOps 模块 8 处 _ = DB 写入改为 log.Printf
Q4 修复了 controller/ 层 8 处 DB 写入的 _ = 静默忽略,但后续合并的 RDP
远程桌面、API Token、AIOps 告警分析等模块引入了新的 _ = DB 写入忽略。
本次将 controller/middleware 层所有 DB 写入操作(Create/Update/Updates)
的 _ = 静默忽略改为带 log.Printf 错误日志(不阻断业务流程)。
修改文件 (6):
- middleware/auth.go: API 密钥最后使用时间更新
- controller/terminal_session.go: 终端回放审计日志 + 修复 log 变量遮蔽
- controller/user_api_token.go: API 密钥默认值补全
- controller/rdp.go: RDP 会话结束状态 + 命令记录
- controller/rdp_session.go: 会话终止 + 回放审计 + 修复 log 变量遮蔽
- controller/aiops_alert_analysis.go: AI 告警分析结果保存
修复 2 处 Go 变量名遮蔽陷阱(log := model.AuditLog{} 遮蔽 "log" 包 → auditLog)
保留 4 处可接受忽略(1 DB 读取 + 3 WebSocket 帧写入)
验证: go build=0, 17/17 集成测试通过, Docker healthy
This commit is contained in:
1 parent
da9fd1e987
commit
cd30af5bd6
8 files changed
+63
-11
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -398,7 +399,9 @@ func (h *AIOpsAlertAnalysisHandler) saveAnalysisTrace(ctx context.Context, in an
|
||||
Duration: in.Duration,
|
||||
Error: in.Error,
|
||||
}
|
||||
_ = h.db.WithContext(ctx).Create(&row).Error
|
||||
if err := h.db.WithContext(ctx).Create(&row).Error; err != nil {
|
||||
log.Printf("[AIOpsAnalysis] 保存告警分析结果失败 (alert_id=%s, provider=%s): %v", in.AlertID, in.Provider, err)
|
||||
}
|
||||
}
|
||||
|
||||
func marshalTraceJSON(v interface{}) string {
|
||||
|
||||
@@ -266,7 +266,9 @@ func (h *RDPHandler) Serve(c *gin.Context) {
|
||||
if errMsg != "" {
|
||||
updates["error"] = errMsg
|
||||
}
|
||||
_ = h.db.Model(&model.RDPSession{}).Where("id = ?", session.ID).Updates(updates).Error
|
||||
if err := h.db.Model(&model.RDPSession{}).Where("id = ?", session.ID).Updates(updates).Error; err != nil {
|
||||
log.Printf("[RDP] 更新会话结束状态失败 (session_id=%d, status=%s): %v", session.ID, status, err)
|
||||
}
|
||||
}()
|
||||
|
||||
audit := newRDPOperationRecorder(h.db, session, userIDPtr, username)
|
||||
@@ -528,7 +530,9 @@ func (r *rdpOperationRecorder) create(command string) {
|
||||
Command: command,
|
||||
Timestamp: time.Now(),
|
||||
}
|
||||
_ = r.db.Create(&cmd).Error
|
||||
if err := r.db.Create(&cmd).Error; err != nil {
|
||||
log.Printf("[RDPAudit] 记录 RDP 操作命令失败 (session_id=%d, user=%s, cmd=%q): %v", r.session.ID, r.username, command, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *RDPHandler) loadRDPCredential(server model.Server) (model.RDPSecretContent, error) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -127,7 +128,9 @@ func (h *RDPSessionHandler) KillRDPSession(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
duration := int(now.Sub(session.StartTime).Seconds())
|
||||
_ = h.db.Model(&session).Updates(map[string]interface{}{"status": "closed", "end_time": now, "duration": duration}).Error
|
||||
if err := h.db.Model(&session).Updates(map[string]interface{}{"status": "closed", "end_time": now, "duration": duration}).Error; err != nil {
|
||||
log.Printf("[RDPSession] 终止会话更新状态失败 (session_id=%d): %v", id, err)
|
||||
}
|
||||
c.JSON(http.StatusOK, model.Response{Code: 200, Message: "已终止"})
|
||||
}
|
||||
|
||||
@@ -160,7 +163,7 @@ func (h *RDPSessionHandler) LogRDPReplayView(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
uid := middleware.GetUserID(c)
|
||||
log := model.AuditLog{
|
||||
auditLog := model.AuditLog{
|
||||
UserID: &uid,
|
||||
Username: middleware.GetUsername(c),
|
||||
IP: c.ClientIP(),
|
||||
@@ -172,6 +175,8 @@ func (h *RDPSessionHandler) LogRDPReplayView(c *gin.Context) {
|
||||
Request: fmt.Sprintf("回放 Windows RDP 会话 #%d(%s → %s/%s)", id, session.Username, session.ServerName, session.ServerIP),
|
||||
Status: 200,
|
||||
}
|
||||
_ = h.db.Create(&log).Error
|
||||
if err := h.db.Create(&auditLog).Error; err != nil {
|
||||
log.Printf("[RDPAudit] 记录回放审计日志失败 (session_id=%d, user=%s): %v", id, session.Username, err)
|
||||
}
|
||||
c.JSON(http.StatusOK, model.Response{Code: 200, Message: "ok"})
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -264,7 +265,7 @@ func (h *TerminalSessionHandler) LogReplayView(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
uid := middleware.GetUserID(c)
|
||||
log := model.AuditLog{
|
||||
auditLog := model.AuditLog{
|
||||
UserID: &uid,
|
||||
Username: middleware.GetUsername(c),
|
||||
IP: c.ClientIP(),
|
||||
@@ -276,7 +277,9 @@ func (h *TerminalSessionHandler) LogReplayView(c *gin.Context) {
|
||||
Request: fmt.Sprintf("回放终端会话 #%d(%s → %s/%s)", id, session.Username, session.ServerName, session.ServerIP),
|
||||
Status: 200,
|
||||
}
|
||||
_ = h.db.Create(&log).Error // 落库失败不阻断回放
|
||||
if err := h.db.Create(&auditLog).Error; err != nil {
|
||||
log.Printf("[TerminalAudit] 记录回放审计日志失败 (session_id=%d, user=%s): %v", id, session.Username, err)
|
||||
}
|
||||
c.JSON(http.StatusOK, model.Response{Code: 200, Message: "ok"})
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package controller
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/big"
|
||||
"net/http"
|
||||
"time"
|
||||
@@ -136,7 +137,9 @@ func (h *UserHandler) ensureAPIKeyMeta(user *model.User) {
|
||||
user.ApiAccessKeyDurationMonths = defaultAPIKeyMonths
|
||||
}
|
||||
if len(patches) > 0 {
|
||||
_ = h.db.Model(&model.User{}).Where("id = ?", user.ID).Updates(patches).Error
|
||||
if err := h.db.Model(&model.User{}).Where("id = ?", user.ID).Updates(patches).Error; err != nil {
|
||||
log.Printf("[UserApiKey] 补全 API 密钥默认值失败 (user_id=%d): %v", user.ID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user