Files
Agent-System/internal/server/chatflow_http.go
T
weichenandCursor f6813cc272 feat(api): add additive HTTP/SSE error envelopes
Public errors now emit code, safe message, and request_id while keeping the legacy error field. Raw SQL, paths, tokens, and XSS payloads are stripped; the frontend branches on code instead of substring matching.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-15 14:40:56 +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:
writeSafeError(w, nil, http.StatusInternalServerError, err.Error())
}
}