Improve practice full-load errors and orphan response filtering.

Surface underlying load failures in the API message, accept pointer practice types, and keep question counts aligned when set items are missing.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Yared Yemane 2026-08-06 16:45:25 +03:00
parent 291e1837c7
commit 9c4636ec83

View File

@ -51,6 +51,34 @@ func fullPracticeQuestionResponses(
return out
}
func asLMSPractice(v interface{}) (domain.Practice, bool) {
switch p := v.(type) {
case domain.Practice:
return p, true
case *domain.Practice:
if p == nil {
return domain.Practice{}, false
}
return *p, true
default:
return domain.Practice{}, false
}
}
func asExamPrepPractice(v interface{}) (domain.ExamPrepPractice, bool) {
switch p := v.(type) {
case domain.ExamPrepPractice:
return p, true
case *domain.ExamPrepPractice:
if p == nil {
return domain.ExamPrepPractice{}, false
}
return *p, true
default:
return domain.ExamPrepPractice{}, false
}
}
func (h *Handler) buildFullPracticeResponse(
ctx context.Context,
result domain.FullUpdatePracticeResult,
@ -60,14 +88,26 @@ func (h *Handler) buildFullPracticeResponse(
if err != nil {
return nil, err
}
questionCount := int64(len(items))
present := make(map[int64]struct{}, len(result.Questions))
for _, q := range result.Questions {
present[q.ID] = struct{}{}
}
// Drop orphaned set membership rows (question deleted from bank) so response
// building stays aligned with the questions we successfully loaded.
filteredItems := make([]domain.QuestionSetItemWithQuestion, 0, len(items))
for _, item := range items {
if _, ok := present[item.QuestionID]; ok {
filteredItems = append(filteredItems, item)
}
}
questionCount := int64(len(filteredItems))
qsRes := questionSetResFromDomain(result.QuestionSet)
qsRes.QuestionCount = &questionCount
return fiber.Map{
"practice": result.Practice,
"question_set": qsRes,
"stimulus_blocks": result.StimulusBlocks,
"questions": fullPracticeQuestionResponses(result.Questions, items, catalog, result.StimulusBlocks),
"practice": result.Practice,
"question_set": qsRes,
"stimulus_blocks": result.StimulusBlocks,
"questions": fullPracticeQuestionResponses(result.Questions, filteredItems, catalog, result.StimulusBlocks),
}, nil
}
@ -110,6 +150,11 @@ func mapPracticeFullError(err error, action string) (int, string) {
return fiber.StatusBadRequest, "Invalid stimulus block"
default:
if action == "load" {
// Include the underlying cause in Message so admin UIs that only
// surface `message` still show enough detail to diagnose.
if err != nil && err.Error() != "" {
return fiber.StatusInternalServerError, "Failed to load practice: " + err.Error()
}
return fiber.StatusInternalServerError, "Failed to load practice"
}
return fiber.StatusBadRequest, "Failed to update practice"
@ -144,10 +189,11 @@ func (h *Handler) GetLmsPracticeFull(c *fiber.Ctx) error {
})
}
practice, ok := result.Practice.(domain.Practice)
practice, ok := asLMSPractice(result.Practice)
if !ok {
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
Message: "Failed to load practice",
Error: fmt.Sprintf("unexpected practice type %T", result.Practice),
})
}
if !practice.VisibleToLearners() && !h.canManageLMSPractices(c) {
@ -213,10 +259,11 @@ func (h *Handler) GetExamPrepPracticeFull(c *fiber.Ctx) error {
})
}
practice, ok := result.Practice.(domain.ExamPrepPractice)
practice, ok := asExamPrepPractice(result.Practice)
if !ok {
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
Message: "Failed to load practice",
Error: fmt.Sprintf("unexpected practice type %T", result.Practice),
})
}
if !practice.VisibleToLearners() && !h.canManageExamPrepPractices(c) {