fix(session): map ErrSessionNotFound to 404 across all handlers
PR #1309 plumbed user-scope into the session/message service layer so non-owner / wrong-tenant lookups now surface as ErrSessionNotFound at every entry point that goes through sessionRepo.Get. The handler-side mapping, however, only existed in 4 of the 8 affected sites: - handler/session/handler.go:GetSession ✓ already mapped (with `==`) - handler/session/handler.go:UpdateSession ✓ already mapped (with `==`) - handler/session/handler.go:DeleteSession ✓ already mapped (with `==`) - handler/session/handler.go:BatchDeleteSessions ✓ already mapped (with `==`) - handler/session/handler.go:ClearSessionMessages ✗ always 500 - handler/session/stream.go:ContinueStream session lookup ✓ (with `==`) - handler/session/stream.go:ContinueStream message lookup ✗ always 500 - handler/message.go:LoadMessages (recent + before-time) ✗ always 500 - handler/message.go:DeleteMessage ✗ always 500 Result: a Contributor in tenant A asking for a Contributor B's session messages got a 500 instead of 404 — the wire response leaked "something is broken" rather than "you can't see this URL", which breaks SDK / frontend axios interceptor error handling that keys on HTTP status. Two changes: 1. Add ErrSessionNotFound → 404 mapping to the 4 sites that lacked it (ClearSessionMessages, ContinueStream's GetMessage, LoadMessages's two paths, DeleteMessage). 2. Replace `err == errors.ErrSessionNotFound` with `stderrors.Is(err, errors.ErrSessionNotFound)` everywhere. Sentinel error comparison must use errors.Is so wrapped errors (`fmt.Errorf("...: %w", ErrSessionNotFound)`) still match. Today no service-layer caller wraps, but a future refactor that does would silently turn 404s into 500s with no test catching it. Tests: - internal/handler/message_session_not_found_test.go covers the four new mapping sites in handler/message.go (LoadMessages-recent, LoadMessages-before-time, DeleteMessage) and pins the wrapped-error behaviour with a dedicated regression test that fmt.Errorf("%w") a sentinel must still be detected as ErrSessionNotFound. The bare handler is hit through gin.Engine + middleware.ErrorHandler so the response shape matches production exactly. - PR #1309's existing service-level tests (session_user_scope_test, session_test on the repository) still pass — this PR is a strictly handler-layer follow-up.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
stderrors "errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/config"
|
||||
@@ -160,7 +161,7 @@ func (h *Handler) GetSession(c *gin.Context) {
|
||||
logger.Infof(ctx, "Retrieving session, ID: %s", id)
|
||||
session, err := h.sessionService.GetSession(ctx, id)
|
||||
if err != nil {
|
||||
if err == errors.ErrSessionNotFound {
|
||||
if stderrors.Is(err, errors.ErrSessionNotFound) {
|
||||
logger.Warnf(ctx, "Session not found, ID: %s", id)
|
||||
c.Error(errors.NewNotFoundError(err.Error()))
|
||||
return
|
||||
@@ -275,7 +276,7 @@ func (h *Handler) UpdateSession(c *gin.Context) {
|
||||
|
||||
// Call service to update session
|
||||
if err := h.sessionService.UpdateSession(ctx, &session); err != nil {
|
||||
if err == errors.ErrSessionNotFound {
|
||||
if stderrors.Is(err, errors.ErrSessionNotFound) {
|
||||
logger.Warnf(ctx, "Session not found, ID: %s", id)
|
||||
c.Error(errors.NewNotFoundError(err.Error()))
|
||||
return
|
||||
@@ -326,7 +327,7 @@ func (h *Handler) DeleteSession(c *gin.Context) {
|
||||
|
||||
// Call service to delete session
|
||||
if err := h.sessionService.DeleteSession(ctx, id); err != nil {
|
||||
if err == errors.ErrSessionNotFound {
|
||||
if stderrors.Is(err, errors.ErrSessionNotFound) {
|
||||
logger.Warnf(ctx, "Session not found, ID: %s", id)
|
||||
c.Error(errors.NewNotFoundError(err.Error()))
|
||||
return
|
||||
@@ -369,6 +370,11 @@ func (h *Handler) ClearSessionMessages(c *gin.Context) {
|
||||
logger.Infof(ctx, "Clearing all messages for session: %s", id)
|
||||
|
||||
if err := h.messageService.ClearSessionMessages(ctx, id); err != nil {
|
||||
if stderrors.Is(err, errors.ErrSessionNotFound) {
|
||||
logger.Warnf(ctx, "Session not found, ID: %s", id)
|
||||
c.Error(errors.NewNotFoundError(err.Error()))
|
||||
return
|
||||
}
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{"session_id": id})
|
||||
c.Error(errors.NewInternalServerError(err.Error()))
|
||||
return
|
||||
@@ -442,7 +448,7 @@ func (h *Handler) BatchDeleteSessions(c *gin.Context) {
|
||||
}
|
||||
|
||||
if err := h.sessionService.BatchDeleteSessions(ctx, sanitizedIDs); err != nil {
|
||||
if err == errors.ErrSessionNotFound {
|
||||
if stderrors.Is(err, errors.ErrSessionNotFound) {
|
||||
logger.Warnf(ctx, "No visible sessions found for batch delete")
|
||||
c.Error(errors.NewNotFoundError(err.Error()))
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user