From 4ad11ea4eee9b1ed02fa97da79931efe92166e92 Mon Sep 17 00:00:00 2001 From: Nate Emmert Date: Fri, 21 Aug 2026 09:06:12 -0400 Subject: [PATCH] Fix test correction: remove transaction, add error handling Replaced db.transaction() wrapper (unreliable in node:sqlite at this version) with a plain loop. Added try/catch in the JS save handler so any server error surfaces as a visible toast instead of failing silently. Co-Authored-By: Claude Sonnet 4.6 --- public/admin.js | 18 +++++++++++------- server.js | 8 +++----- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/public/admin.js b/public/admin.js index e5bd532..9e15bf3 100644 --- a/public/admin.js +++ b/public/admin.js @@ -2119,13 +2119,17 @@ $('#correct-test-save').addEventListener('click', async () => { wordId: Number(cb.dataset.wordId), correct: cb.checked, })); - const { score, total } = await api(`/api/tests/${correctTestId}/correct`, { - method: 'PATCH', - body: { corrections }, - }); - $('#correct-test-modal').hidden = true; - msg(`Score updated: ${score}/${total}`); - loadSpellingTests(); + try { + const { score, total } = await api(`/api/tests/${correctTestId}/correct`, { + method: 'PATCH', + body: { corrections }, + }); + $('#correct-test-modal').hidden = true; + msg(`Score updated: ${score}/${total}`); + loadSpellingTests(); + } catch (err) { + msg(err.message || 'Failed to save corrections', 'error'); + } }); $('#gradebook-course').addEventListener('change', () => renderGradebook($('#gradebook-course').value)); diff --git a/server.js b/server.js index c517966..3adb905 100644 --- a/server.js +++ b/server.js @@ -539,11 +539,9 @@ app.patch('/api/tests/:testId/correct', requirePin, (req, res) => { if (!test) return res.status(404).json({ error: 'Test not found' }); const upd = db.prepare(`UPDATE test_answers SET correct = ? WHERE test_id = ? AND word_id = ?`); - db.transaction(() => { - for (const c of corrections) { - upd.run(c.correct ? 1 : 0, testId, c.wordId); - } - })(); + for (const c of corrections) { + upd.run(c.correct ? 1 : 0, testId, c.wordId); + } const newScore = db.prepare(`SELECT COUNT(*) AS n FROM test_answers WHERE test_id = ? AND correct = 1`).get(testId).n; db.prepare(`UPDATE tests SET score = ? WHERE id = ?`).run(newScore, testId);