🐛 修正 ws 错误状体的返回。

This commit is contained in:
Havoc412 2024-11-18 00:16:45 +08:00
parent 0c2903e4f8
commit 5ca6966b2f
4 changed files with 18 additions and 5 deletions

View File

@ -15,6 +15,6 @@ const (
ErrGeneralStart = ErrGeneral + iota
ErrInvalidData
ErrInternalError
ErrDataNoFound
ErrServerDown
)

View File

@ -19,6 +19,7 @@ func init() {
// INGO
ErrMsgForUser = make(msg)
GeneralMsgUserInit(ErrMsgForUser)
AnimalMsgUserInit(ErrMsgForUser)
EncounterMsgUserInit(ErrMsgForUser)
KnowledgeMsgUserInit(ErrMsgForUser)
@ -31,3 +32,7 @@ func GeneralMsgInit(m msg) {
m[ErrInternalError] = "内部服务器错误"
m[ErrDataNoFound] = "无数据查询"
}
func GeneralMsgUserInit(m msg) {
m[ErrServerDown] = "后端服务未启动,此功能暂时无法使用。"
}

View File

@ -12,5 +12,5 @@ func NlpMsgInit(m msg) {
func NlpMsgUserInit(m msg) {
m[ErrNoContent] = "请输入内容"
m[ErrNoDocFound] = "没有找到相关文档"
m[ErrNoDocFound] = "小护没有在知识库中找到相关文档。😿😿😿"
}

View File

@ -6,6 +6,7 @@ import (
"catface/app/model_es"
"catface/app/service/nlp"
"catface/app/utils/response"
"fmt"
"io"
"net/http"
@ -117,8 +118,11 @@ func (r *Rag) ChatWebSocket(context *gin.Context) {
// 1. query embedding
embedding, ok := nlp.GetEmbedding(query)
if !ok {
code := errcode.ErrPythonService
response.Fail(context, code, errcode.ErrMsg[code], "")
code := errcode.ErrServerDown
err := ws.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf("%d:%s", code, errcode.ErrMsgForUser[code])))
if err != nil {
variable.ZapLog.Error("Failed to send error message via WebSocket", zap.Error(err))
}
return
}
@ -128,7 +132,11 @@ func (r *Rag) ChatWebSocket(context *gin.Context) {
variable.ZapLog.Error("ES TopK error", zap.Error(err))
code := errcode.ErrNoDocFound
response.Fail(context, code, errcode.ErrMsg[code], errcode.ErrMsgForUser[code])
err := ws.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf("%d:%s", code, errcode.ErrMsgForUser[code])))
if err != nil {
variable.ZapLog.Error("Failed to send error message via WebSocket", zap.Error(err))
}
return
}
// 3.