chore: error recover is enable ONLY in debug mode

This commit is contained in:
RockYang 2024-01-06 17:16:02 +08:00
parent 60a3751839
commit 9909c3a0ed
2 changed files with 15 additions and 11 deletions

View File

@ -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 {
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)

View File

@ -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