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" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
"github.com/gorilla/websocket"
"gorm.io/gorm"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
"time" "time"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
"github.com/gorilla/websocket"
"gorm.io/gorm"
) )
const ErrorMsg = "抱歉AI 助手开小差了,请稍后再试。" 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 { func (h *ChatHandler) sendMessage(ctx context.Context, session *types.ChatSession, role model.ChatRole, prompt string, ws *types.WsClient) error {
defer func() { if !h.App.Debug {
if r := recover(); r != nil { defer func() {
logger.Error("Recover message from error: ", r) if r := recover(); r != nil {
} logger.Error("Recover message from error: ", r)
}() }
}()
}
var user model.User var user model.User
res := h.db.Model(&model.User{}).First(&user, session.UserId) res := h.db.Model(&model.User{}).First(&user, session.UserId)

View File

@ -9,12 +9,13 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
req2 "github.com/imroc/req/v3"
"html/template" "html/template"
"io" "io"
"strings" "strings"
"time" "time"
"unicode/utf8" "unicode/utf8"
req2 "github.com/imroc/req/v3"
) )
// OPenAI 消息发送实现 // OPenAI 消息发送实现
@ -137,7 +138,7 @@ func (h *ChatHandler) sendOpenAiMessage(
if err != nil { if err != nil {
errMsg = err.Error() errMsg = err.Error()
} else if r.IsErrorState() { } else if r.IsErrorState() {
errMsg = r.Err.Error() errMsg = r.Status
} }
if errMsg != "" || apiRes.Code != types.Success { if errMsg != "" || apiRes.Code != types.Success {
msg := "调用函数工具出错:" + apiRes.Message + errMsg msg := "调用函数工具出错:" + apiRes.Message + errMsg