Skip to content

Commit 55a0855

Browse files
committed
Fix tag creation error handling (api returns error, doesn't throw)
1 parent 8bd9407 commit 55a0855

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

questionbank/static/js/app.js

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2993,35 +2993,54 @@
29932993
return;
29942994
}
29952995

2996-
try {
2997-
const res = await api('tags/', 'POST', { name: trimmedName });
2998-
// Add the new tag to the dropdown with ID as data attribute
2996+
// Check if tag already exists in allTags
2997+
let existingTag = allTags.find(t => t.name === trimmedName);
2998+
if (existingTag) {
29992999
const opt = document.createElement('option');
30003000
opt.value = trimmedName;
30013001
opt.textContent = trimmedName;
3002-
opt.dataset.tagId = res.id; // Store the ID
3002+
opt.dataset.tagId = existingTag.id;
30033003
tagSelect.insertBefore(opt, tagSelect.lastChild);
30043004
tagSelect.value = trimmedName;
3005-
// Add to allTags so lookup works
3006-
allTags.push({ id: res.id, name: res.name });
3007-
} catch (e) {
3008-
// If tag already exists, try to find and select it
3009-
if (e.message && e.message.includes('already exists')) {
3010-
await loadTags(); // Refresh to get the existing tag
3011-
const existingTag = allTags.find(t => t.name === trimmedName);
3012-
if (existingTag) {
3013-
const opt = document.createElement('option');
3014-
opt.value = trimmedName;
3015-
opt.textContent = trimmedName;
3016-
opt.dataset.tagId = existingTag.id;
3017-
tagSelect.insertBefore(opt, tagSelect.lastChild);
3018-
tagSelect.value = trimmedName;
3019-
return;
3020-
}
3005+
return;
3006+
}
3007+
3008+
const res = await api('tags/', 'POST', { name: trimmedName });
3009+
3010+
// Check if response indicates error (400 returns error object)
3011+
if (res.name && res.name[0] && res.name[0].includes('already exists')) {
3012+
// Tag exists - reload tags and find it
3013+
await loadTags();
3014+
existingTag = allTags.find(t => t.name === trimmedName);
3015+
if (existingTag) {
3016+
const opt = document.createElement('option');
3017+
opt.value = trimmedName;
3018+
opt.textContent = trimmedName;
3019+
opt.dataset.tagId = existingTag.id;
3020+
tagSelect.insertBefore(opt, tagSelect.lastChild);
3021+
tagSelect.value = trimmedName;
3022+
return;
30213023
}
3022-
alert('Failed to create tag: ' + e.message);
3024+
alert('Tag exists but could not be found. Please refresh the page.');
30233025
tagSelect.value = '';
3026+
return;
30243027
}
3028+
3029+
if (!res.id) {
3030+
console.error('[Tag] Unexpected response:', res);
3031+
alert('Failed to create tag');
3032+
tagSelect.value = '';
3033+
return;
3034+
}
3035+
3036+
// Success - add the new tag
3037+
const opt = document.createElement('option');
3038+
opt.value = trimmedName;
3039+
opt.textContent = trimmedName;
3040+
opt.dataset.tagId = res.id;
3041+
tagSelect.insertBefore(opt, tagSelect.lastChild);
3042+
tagSelect.value = trimmedName;
3043+
allTags.push({ id: res.id, name: res.name });
30253044
} else {
30263045
tagSelect.value = '';
30273046
}

0 commit comments

Comments
 (0)