解决聊天页面异常退出重连的bug

This commit is contained in:
GeekMaster
2025-07-17 11:47:17 +08:00
parent 545f257476
commit 1d6f0ab714
4 changed files with 77 additions and 2 deletions

View File

@@ -95,6 +95,15 @@ func (h *ChatHandler) Chat(c *gin.Context) {
ctx, cancel := context.WithCancel(c.Request.Context())
defer cancel()
// 这里做个全局的异常处理,防止整个请求异常,导致 SSE 连接断开
defer func() {
if err := recover(); err != nil {
logger.Errorf("chat handler error: %v", err)
pushMessage(c, ChatEventError, err)
c.Abort()
}
}()
// 使用旧的聊天数据覆盖模型和角色ID
var chat model.ChatItem
h.DB.Where("chat_id", input.ChatId).First(&chat)