38 lines
1.2 KiB
Go
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)
|
|
}
|
|
}
|