From 78b60b8a525869fb4a9e838d2984be449c16119b Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Thu, 6 Aug 2026 12:59:36 +0300 Subject: [PATCH] Fix practice full load when question set has orphaned items. Skip missing questions instead of failing the GET so admins can open and repair practices; map stray ErrNoRows to a clearer 404. Co-authored-by: Cursor --- internal/services/practicecontent/service.go | 9 +++++++-- .../web_server/handlers/practice_full_update_handler.go | 3 +++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/internal/services/practicecontent/service.go b/internal/services/practicecontent/service.go index 041df72f..4cf2bc40 100644 --- a/internal/services/practicecontent/service.go +++ b/internal/services/practicecontent/service.go @@ -162,15 +162,20 @@ func (s *Service) loadQuestionsInSet(ctx context.Context, questionSetID int64) ( if err != nil { return nil, nil, err } + // Keep set membership aligned with questions that still exist. Orphaned + // question_set_items (deleted questions) must not fail the whole practice GET — + // admin editors need to open and repair those shells. + keptItems := make([]domain.QuestionSetItemWithQuestion, 0, len(items)) questions := make([]domain.QuestionWithDetails, 0, len(items)) for _, item := range items { q, ok := byID[item.QuestionID] if !ok { - return nil, nil, pgx.ErrNoRows + continue } + keptItems = append(keptItems, item) questions = append(questions, q) } - return items, questions, nil + return keptItems, questions, nil } func (s *Service) loadPracticeFullContent(ctx context.Context, questionSetID int64) (domain.QuestionSet, []domain.QuestionWithDetails, error) { diff --git a/internal/web_server/handlers/practice_full_update_handler.go b/internal/web_server/handlers/practice_full_update_handler.go index 44b3e5d0..7c1dcc89 100644 --- a/internal/web_server/handlers/practice_full_update_handler.go +++ b/internal/web_server/handlers/practice_full_update_handler.go @@ -10,6 +10,7 @@ import ( "strconv" "github.com/gofiber/fiber/v2" + "github.com/jackc/pgx/v5" ) func fullPracticeQuestionResponses( @@ -97,6 +98,8 @@ func mapPracticeFullError(err error, action string) (int, string) { return fiber.StatusNotFound, "Question set not found" case errors.Is(err, practicecontent.ErrQuestionNotFound): return fiber.StatusNotFound, "Question not found" + case errors.Is(err, pgx.ErrNoRows): + return fiber.StatusNotFound, "Practice content not found" case errors.Is(err, domain.ErrPersonaNotFound): return fiber.StatusNotFound, "Persona not found" case errors.Is(err, practicecontent.ErrInvalidQuestionItem):