xonecas.io
53129
Pa

PAT / commit 4c245a4

fix: add error logging to API route catch blocks

xonecas2025-12-19 4c245a4958bb8690888548f89ec785bef2d53b1d parent a9a4c48

4 files changed

app/api/models/route.ts+3−1
@@ -1,5 +1,6 @@
11 import { NextResponse } from "next/server";
22 import ollama from "ollama";
3+import { debug } from "@/src/lib/debug";
34
45 export async function GET() {
56 try {
@@ -9,7 +10,8 @@ export async function GET() {
910 model: model.name, // Use name as the model identifier
1011 }));
1112 return NextResponse.json({ models });
12- } catch {
13+ } catch (err) {
14+ debug(`[Models API] Error fetching models:`, err instanceof Error ? err.message : "Unknown error");
1315 return NextResponse.json({ error: "Failed to fetch models" }, { status: 500 });
1416 }
1517 }
app/api/threads/[id]/messages/route.ts+8−2
@@ -2,11 +2,13 @@ import { NextResponse } from "next/server";
22 import { db } from "@/src/lib/db";
33 import { messages } from "@/src/lib/db/schema";
44 import { eq, asc, and, notInArray } from "drizzle-orm";
5+import { debug } from "@/src/lib/debug";
56
67 export async function GET(request: Request, { params }: { params: Promise<{ id: string }> }) {
8+ let threadId: number | null = null;
79 try {
810 const { id } = await params;
9- const threadId = parseInt(id);
11+ threadId = parseInt(id);
1012 if (isNaN(threadId)) {
1113 return NextResponse.json({ error: "Invalid thread ID" }, { status: 400 });
1214 }
@@ -18,7 +20,11 @@ export async function GET(request: Request, { params }: { params: Promise<{ id:
1820 .orderBy(asc(messages.createdAt));
1921
2022 return NextResponse.json({ messages: messagesList });
21- } catch {
23+ } catch (err) {
24+ debug(
25+ `[Messages API] Error fetching messages for thread ${threadId ?? "unknown"}:`,
26+ err instanceof Error ? err.message : "Unknown error"
27+ );
2228 return NextResponse.json({ error: "Failed to get messages" }, { status: 500 });
2329 }
2430 }
app/api/threads/[id]/route.ts+15−4
@@ -2,11 +2,13 @@ import { NextResponse } from "next/server";
22 import { db } from "@/src/lib/db";
33 import { threads } from "@/src/lib/db/schema";
44 import { eq } from "drizzle-orm";
5+import { debug } from "@/src/lib/debug";
56
67 export async function PATCH(request: Request, { params }: { params: Promise<{ id: string }> }) {
8+ let threadId: number | null = null;
79 try {
810 const { id } = await params;
9- const threadId = parseInt(id);
11+ threadId = parseInt(id);
1012 if (isNaN(threadId)) {
1113 return NextResponse.json({ error: "Invalid thread ID" }, { status: 400 });
1214 }
@@ -55,15 +57,20 @@ export async function PATCH(request: Request, { params }: { params: Promise<{ id
5557 }
5658
5759 return NextResponse.json({ thread: updatedThread[0] });
58- } catch {
60+ } catch (err) {
61+ debug(
62+ `[Threads API] Error updating thread ${threadId ?? "unknown"}:`,
63+ err instanceof Error ? err.message : "Unknown error"
64+ );
5965 return NextResponse.json({ error: "Failed to update thread" }, { status: 500 });
6066 }
6167 }
6268
6369 export async function DELETE(request: Request, { params }: { params: Promise<{ id: string }> }) {
70+ let threadId: number | null = null;
6471 try {
6572 const { id } = await params;
66- const threadId = parseInt(id);
73+ threadId = parseInt(id);
6774 if (isNaN(threadId)) {
6875 return NextResponse.json({ error: "Invalid thread ID" }, { status: 400 });
6976 }
@@ -76,7 +83,11 @@ export async function DELETE(request: Request, { params }: { params: Promise<{ i
7683 }
7784
7885 return NextResponse.json({ success: true });
79- } catch {
86+ } catch (err) {
87+ debug(
88+ `[Threads API] Error deleting thread ${threadId ?? "unknown"}:`,
89+ err instanceof Error ? err.message : "Unknown error"
90+ );
8091 return NextResponse.json({ error: "Failed to delete thread" }, { status: 500 });
8192 }
8293 }
app/api/threads/route.ts+5−2
@@ -3,6 +3,7 @@ import { db } from "@/src/lib/db";
33 import { threads, messages } from "@/src/lib/db/schema";
44 import { desc, count } from "drizzle-orm";
55 import { generateSystemPrompt } from "@/src/lib/ollama/chat/system-prompt";
6+import { debug } from "@/src/lib/debug";
67
78 export async function POST(request: Request) {
89 try {
@@ -47,7 +48,8 @@ export async function POST(request: Request) {
4748 });
4849
4950 return NextResponse.json({ thread: newThread[0] });
50- } catch {
51+ } catch (err) {
52+ debug(`[Threads API] Error creating thread:`, err instanceof Error ? err.message : "Unknown error");
5153 return NextResponse.json({ error: "Failed to create thread" }, { status: 500 });
5254 }
5355 }
@@ -70,7 +72,8 @@ export async function GET(request: Request) {
7072 const hasMore = offset + limit < totalCount;
7173
7274 return NextResponse.json({ threads: threadsList, hasMore, totalCount });
73- } catch {
75+ } catch (err) {
76+ debug(`[Threads API] Error fetching threads:`, err instanceof Error ? err.message : "Unknown error");
7477 return NextResponse.json({ error: "Failed to get threads" }, { status: 500 });
7578 }
7679 }