Skip to content

Commit 5617e1e

Browse files
committed
Add detailed logging for exercise caching issues
- Add comprehensive logging to saveExercise function - Add detailed logging to tryGenerateAndCacheExercise function - Improve error messages with more context - Help diagnose French language caching failures This will help identify why exercises are failing to save to cache with undefined IDs for certain languages.
1 parent 56690d0 commit 5617e1e

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

app/actions/exercise.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,20 +195,37 @@ const tryGenerateAndCacheExercise = async (
195195
userId: number | null
196196
): Promise<Result<{ content: ExerciseContent; id: number }, ActionError>> => {
197197
try {
198+
console.log('[Exercise] Starting generation for:', {
199+
passageLanguage: params.passageLanguage,
200+
questionLanguage: params.questionLanguage,
201+
level: params.level,
202+
userId,
203+
});
204+
198205
const options: ExerciseGenerationOptions = { ...params, language: params.passageLanguage };
199206
const generatedExercise = await generateAndValidateExercise(options);
207+
208+
console.log('[Exercise] AI generation successful, attempting to save to cache...');
209+
200210
const exerciseId = saveExerciseToCache(
201211
params.passageLanguage,
202212
params.questionLanguage,
203213
params.level,
204214
JSON.stringify(generatedExercise),
205215
userId
206216
);
217+
207218
if (typeof exerciseId !== 'number') {
208-
return failure({ error: 'Exercise generated but failed to save to cache (undefined ID).' });
219+
console.error('[Exercise] Failed to save to cache - exerciseId is not a number:', exerciseId);
220+
return failure({
221+
error: `Exercise generated but failed to save to cache (undefined ID). Generated exercise: ${JSON.stringify(generatedExercise).substring(0, 100)}...`,
222+
});
209223
}
224+
225+
console.log('[Exercise] Successfully saved exercise with ID:', exerciseId);
210226
return success({ content: generatedExercise, id: exerciseId });
211227
} catch (error) {
228+
console.error('[Exercise] Error during generation:', error);
212229
return failure({
213230
error: `Error during AI generation/processing: ${error instanceof Error ? error.message : String(error)}`,
214231
});

app/repo/quizRepo.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,35 @@ export const saveExercise = (
109109
userId: number | null
110110
): number | undefined => {
111111
try {
112+
console.log('[QuizRepository] Attempting to save exercise:', {
113+
passageLanguage,
114+
questionLanguage,
115+
level,
116+
contentLength: contentJson.length,
117+
userId,
118+
});
119+
112120
const result = db
113121
.prepare(
114122
'INSERT INTO quiz (language, level, content, question_language, user_id, created_at) VALUES (?, ?, ?, ?, ?, datetime("now")) RETURNING id'
115123
)
116124
.get(passageLanguage, level, contentJson, questionLanguage, userId) as
117125
| { id: number }
118126
| undefined;
119-
return result?.id;
127+
128+
if (!result || typeof result.id !== 'number') {
129+
console.error('[QuizRepository] Failed to get ID from database insert:', result);
130+
return undefined;
131+
}
132+
133+
console.log('[QuizRepository] Successfully saved exercise with ID:', result.id);
134+
return result.id;
120135
} catch (error) {
121136
console.error('[QuizRepository] Error saving exercise:', error);
137+
console.error('[QuizRepository] Error details:', {
138+
message: error instanceof Error ? error.message : String(error),
139+
stack: error instanceof Error ? error.stack : undefined,
140+
});
122141
return undefined;
123142
}
124143
};

0 commit comments

Comments
 (0)