From 76c510109289c28e3b8bef813838d45e31b8a548 Mon Sep 17 00:00:00 2001 From: RockYang Date: Sat, 6 Jan 2024 17:16:02 +0800 Subject: [PATCH] chore: error recover is enable ONLY in debug mode --- api/handler/chatimpl/chat_handler.go | 21 ++++++++++++--------- api/handler/chatimpl/openai_handler.go | 5 +++-- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/api/handler/chatimpl/chat_handler.go b/api/handler/chatimpl/chat_handler.go index 03512333..cffcd67e 100644 --- a/api/handler/chatimpl/chat_handler.go +++ b/api/handler/chatimpl/chat_handler.go @@ -14,14 +14,15 @@ import ( "encoding/json" "errors" "fmt" - "github.com/gin-gonic/gin" - "github.com/go-redis/redis/v8" - "github.com/gorilla/websocket" - "gorm.io/gorm" "net/http" "net/url" "strings" "time" + + "github.com/gin-gonic/gin" + "github.com/go-redis/redis/v8" + "github.com/gorilla/websocket" + "gorm.io/gorm" ) const ErrorMsg = "抱歉,AI 助手开小差了,请稍后再试。" @@ -156,11 +157,13 @@ func (h *ChatHandler) ChatHandle(c *gin.Context) { } func (h *ChatHandler) sendMessage(ctx context.Context, session *types.ChatSession, role model.ChatRole, prompt string, ws *types.WsClient) error { - defer func() { - if r := recover(); r != nil { - logger.Error("Recover message from error: ", r) - } - }() + if !h.App.Debug { + defer func() { + if r := recover(); r != nil { + logger.Error("Recover message from error: ", r) + } + }() + } var user model.User res := h.db.Model(&model.User{}).First(&user, session.UserId) diff --git a/api/handler/chatimpl/openai_handler.go b/api/handler/chatimpl/openai_handler.go index 84a8bcc8..371fa38c 100644 --- a/api/handler/chatimpl/openai_handler.go +++ b/api/handler/chatimpl/openai_handler.go @@ -9,12 +9,13 @@ import ( "context" "encoding/json" "fmt" - req2 "github.com/imroc/req/v3" "html/template" "io" "strings" "time" "unicode/utf8" + + req2 "github.com/imroc/req/v3" ) // OPenAI 消息发送实现 @@ -137,7 +138,7 @@ func (h *ChatHandler) sendOpenAiMessage( if err != nil { errMsg = err.Error() } else if r.IsErrorState() { - errMsg = r.Err.Error() + errMsg = r.Status } if errMsg != "" || apiRes.Code != types.Success { msg := "调用函数工具出错:" + apiRes.Message + errMsg