Improve error handling and validation in importantVocab computed property of VocabLessonView

- Added checks to ensure importantVocab is only processed if lesson and grammarExercises are valid.
- Enhanced error handling with try-catch blocks to log issues during vocabulary extraction, improving robustness.
- Updated the condition for rendering the vocabulary list to prevent errors when importantVocab is undefined.
This commit is contained in:
Torsten Schulz (local)
2026-01-19 19:01:49 +01:00
parent 196b74bebb
commit 0331ffeb93

View File

@@ -39,7 +39,7 @@
</div> </div>
<!-- Wichtige Begriffe aus den Übungen --> <!-- Wichtige Begriffe aus den Übungen -->
<div v-if="importantVocab.length > 0" class="vocab-list"> <div v-if="importantVocab && importantVocab.length > 0" class="vocab-list">
<h4>{{ $t('socialnetwork.vocab.courses.importantVocab') }}</h4> <h4>{{ $t('socialnetwork.vocab.courses.importantVocab') }}</h4>
<div class="vocab-items"> <div class="vocab-items">
<div v-for="(vocab, index) in importantVocab" :key="index" class="vocab-item"> <div v-for="(vocab, index) in importantVocab" :key="index" class="vocab-item">
@@ -196,41 +196,52 @@ export default {
}, },
importantVocab() { importantVocab() {
// Extrahiere wichtige Begriffe aus den Übungen // Extrahiere wichtige Begriffe aus den Übungen
if (!this.lesson || !this.lesson.grammarExercises) return []; try {
if (!this.lesson || !this.lesson.grammarExercises || !Array.isArray(this.lesson.grammarExercises)) {
const vocabMap = new Map(); return [];
this.lesson.grammarExercises.forEach(exercise => {
// Extrahiere aus questionData
const qData = this.getQuestionData(exercise);
const aData = this.getAnswerData(exercise);
if (qData && aData) {
// Für Multiple Choice: Extrahiere Optionen und richtige Antwort
if (this.getExerciseType(exercise) === 'multiple_choice') {
const correct = Array.isArray(aData.correct) ? aData.correct[0] : aData.correct;
const question = qData.text || '';
// Versuche die Frage zu analysieren (z.B. "Wie sagt man X auf Bisaya?")
const match = question.match(/['"]([^'"]+)['"]/);
if (match) {
const germanWord = match[1];
vocabMap.set(correct, { learning: correct, reference: germanWord });
} else if (correct) {
// Fallback: Verwende die richtige Antwort als Lernwort
vocabMap.set(correct, { learning: correct, reference: correct });
}
}
// Für Gap Fill: Extrahiere richtige Antworten
if (this.getExerciseType(exercise) === 'gap_fill' && aData.correct) {
const correct = Array.isArray(aData.correct) ? aData.correct[0] : aData.correct;
vocabMap.set(correct, { learning: correct, reference: correct });
}
} }
});
return Array.from(vocabMap.values()); const vocabMap = new Map();
this.lesson.grammarExercises.forEach(exercise => {
try {
// Extrahiere aus questionData
const qData = this.getQuestionData(exercise);
const aData = this.getAnswerData(exercise);
if (qData && aData) {
// Für Multiple Choice: Extrahiere Optionen und richtige Antwort
if (this.getExerciseType(exercise) === 'multiple_choice') {
const correct = Array.isArray(aData.correct) ? aData.correct[0] : aData.correct;
const question = qData.text || '';
// Versuche die Frage zu analysieren (z.B. "Wie sagt man X auf Bisaya?")
const match = question.match(/['"]([^'"]+)['"]/);
if (match) {
const germanWord = match[1];
vocabMap.set(correct, { learning: correct, reference: germanWord });
} else if (correct) {
// Fallback: Verwende die richtige Antwort als Lernwort
vocabMap.set(correct, { learning: correct, reference: correct });
}
}
// Für Gap Fill: Extrahiere richtige Antworten
if (this.getExerciseType(exercise) === 'gap_fill' && aData.correct) {
const correct = Array.isArray(aData.correct) ? aData.correct[0] : aData.correct;
vocabMap.set(correct, { learning: correct, reference: correct });
}
}
} catch (e) {
console.warn('Fehler beim Extrahieren von Vokabeln aus Übung:', e);
}
});
return Array.from(vocabMap.values());
} catch (e) {
console.error('Fehler in importantVocab computed property:', e);
return [];
}
} }
}, },
computed: { computed: {