Files
Agent-System/internal/server/chatflow_http.go
T
weichen f3f23a3018 feat(errcode): 用稳定错误码记录 HTTP 与异步失败
失败响应此前不写日志,request_id 无法对照原因。注册表不覆盖处理器已经给出的 HTTP 状态。
2026-09-28 02:27:16 +00:00

38 lines
1.2 KiB
Go

package server
import (
"errors"
"net/http"
"chat-agent-system/internal/app/chatflow"
"chat-agent-system/internal/auth"
)
func (s *Server) chatPrincipal(user *auth.UserAccount, action chatflow.Action) chatflow.Principal {
if user == nil {
return chatflow.Principal{Action: action}
}
return chatflow.Principal{UserID: user.ID, Action: action}
}
func (s *Server) writeChatLogError(w http.ResponseWriter, err error) {
switch {
case errors.Is(err, chatflow.ErrUnauthenticated):
writeSafeError(w, nil, http.StatusUnauthorized, `{"error":"ERR_UNAUTHENTICATED","message":"请先登录"}`)
case errors.Is(err, chatflow.ErrForbidden):
writeJSONResponse(w, http.StatusForbidden, map[string]string{
"error_code": "SESSION_FORBIDDEN",
"message": "无权访问该会话",
})
case errors.Is(err, chatflow.ErrNotFound):
writeJSONResponse(w, http.StatusNotFound, map[string]string{
"error_code": "SESSION_NOT_FOUND",
"message": "指定会话不存在或已被删除",
})
case errors.Is(err, chatflow.ErrClosed):
writeSafeError(w, nil, http.StatusServiceUnavailable, "context manager not initialized")
default:
writeDomainError(w, nil, http.StatusInternalServerError, err)
}
}