harden database interactions and improve error handling

This commit is contained in:
ImBenji
2026-04-28 14:36:13 +01:00
parent 5a4caaf1d0
commit e1f168a302
29 changed files with 869 additions and 241 deletions
+20
View File
@@ -0,0 +1,20 @@
export default defineNitroPlugin(() => {
const config = useRuntimeConfig();
const missing: string[] = [];
if (!config.openrouterApiKey) missing.push("NUXT_OPENROUTER_API_KEY");
const provider = (config.ttsProvider as string || "elevenlabs").toLowerCase();
if (provider === "fishaudio") {
if (!config.fishAudioApiKey) missing.push("NUXT_FISH_AUDIO_API_KEY");
} else {
if (!config.elevenlabsApiKey) missing.push("NUXT_ELEVENLABS_API_KEY");
}
if (missing.length > 0) {
console.error("[revisione] Missing required env vars:", missing.join(", "));
process.exit(1);
}
console.log("[revisione] env validation passed");
});
+9
View File
@@ -0,0 +1,9 @@
export default defineNitroPlugin((nitro) => {
nitro.hooks.hook("error", async (error, { event }) => {
// only log server-side, never expose internals to client
const status = (error as any)?.statusCode ?? 500;
if (status >= 500) {
console.error("[revisione] unhandled error:", error?.message ?? error);
}
});
});
+22 -1
View File
@@ -1,11 +1,32 @@
import { db } from "../db/index";
import { courses } from "../db/schema";
import { courses, topics, lessons } from "../db/schema";
import { eq } from "drizzle-orm";
import { generateCourseInBackground } from "../utils/generateCourse";
export default defineNitroPlugin(async () => {
console.log("[revisione] resumeGeneration plugin started");
// reset any stuck generating states from a previous run
try {
const stuckTopics = await db
.update(topics)
.set({ status: "pending" })
.where(eq(topics.status, "generating"))
.returning({ id: topics.id });
const stuckLessons = await db
.update(lessons)
.set({ branchStatus: "pending" })
.where(eq(lessons.branchStatus, "generating"))
.returning({ id: lessons.id });
if (stuckTopics.length > 0 || stuckLessons.length > 0) {
console.log(`[revisione] reset ${stuckTopics.length} stuck topic(s) and ${stuckLessons.length} stuck lesson(s) to pending`);
}
} catch (err: any) {
console.error("[revisione] failed to reset stuck states:", err?.message ?? err);
}
try {
const stuck = await db.query.courses.findMany({
where: eq(courses.status, "processing"),