Skip to content

Commit 23a39f8

Browse files
committed
Add debug API endpoints for database connection and todos table validation
- Introduced a new debug endpoint to test database connectivity and check the existence of the 'todos' table. - Enhanced the main page to display debug information regarding the retrieval of the DATABASE_URL. - Improved error handling and logging in the todos API for better troubleshooting.
1 parent 22118c0 commit 23a39f8

File tree

4 files changed

+126
-6
lines changed

4 files changed

+126
-6
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { db } from "@/db";
2+
import { NextResponse } from "next/server";
3+
4+
export async function GET() {
5+
try {
6+
// Test basic database connection
7+
const result = await db.execute("SELECT 1 as test");
8+
9+
return NextResponse.json({
10+
status: "success",
11+
message: "Database connection works",
12+
result: result.rows,
13+
});
14+
} catch (error) {
15+
return NextResponse.json(
16+
{
17+
status: "error",
18+
error: error instanceof Error ? error.message : String(error),
19+
stack: error instanceof Error ? error.stack : undefined,
20+
},
21+
{ status: 500 }
22+
);
23+
}
24+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { db } from "@/db";
2+
import { NextResponse } from "next/server";
3+
4+
export async function GET() {
5+
try {
6+
// Check if todos table exists
7+
const tableExists = await db.execute(`
8+
SELECT EXISTS (
9+
SELECT FROM information_schema.tables
10+
WHERE table_schema = 'public'
11+
AND table_name = 'todos'
12+
);
13+
`);
14+
15+
// Try to count todos
16+
let todoCount = null;
17+
let todoCountError = null;
18+
19+
try {
20+
const countResult = await db.execute(
21+
"SELECT COUNT(*) as count FROM todos"
22+
);
23+
todoCount = countResult.rows[0];
24+
} catch (error) {
25+
todoCountError = error instanceof Error ? error.message : String(error);
26+
}
27+
28+
return NextResponse.json({
29+
status: "success",
30+
tableExists: tableExists.rows[0],
31+
todoCount,
32+
todoCountError,
33+
});
34+
} catch (error) {
35+
return NextResponse.json(
36+
{
37+
status: "error",
38+
error: error instanceof Error ? error.message : String(error),
39+
stack: error instanceof Error ? error.stack : undefined,
40+
},
41+
{ status: 500 }
42+
);
43+
}
44+
}

apps/web/src/app/api/todos/route.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,34 @@ import { NextResponse } from "next/server";
55
// GET /api/todos - List all todos
66
export async function GET() {
77
try {
8+
console.log("Starting todos fetch...");
9+
10+
// Test database connection first
11+
console.log("Testing basic database connection...");
12+
await db.execute("SELECT 1");
13+
console.log("Database connection OK");
14+
15+
// Try the actual query
16+
console.log("Executing todos query...");
817
const allTodos = await db
918
.select()
1019
.from(todos)
1120
.orderBy(desc(todos.createdAt));
21+
22+
console.log(`Found ${allTodos.length} todos`);
1223
return NextResponse.json(allTodos);
1324
} catch (error) {
14-
console.error("Failed to fetch todos:", error);
25+
console.error("Detailed error in todos fetch:", {
26+
message: error instanceof Error ? error.message : String(error),
27+
stack: error instanceof Error ? error.stack : undefined,
28+
name: error instanceof Error ? error.name : undefined,
29+
});
30+
1531
return NextResponse.json(
16-
{ error: "Failed to fetch todos" },
32+
{
33+
error: "Failed to fetch todos",
34+
details: error instanceof Error ? error.message : String(error),
35+
},
1736
{ status: 500 }
1837
);
1938
}

apps/web/src/app/page.tsx

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import TodoApp from "./components/TodoApp";
44
function redactDatabaseUrl(url: string): string {
55
if (!url) return "No DATABASE_URL found";
66

7-
// Parse the URL to extract components
87
try {
98
const parsed = new URL(url);
109
const host = parsed.hostname;
1110
const port = parsed.port ? `:${parsed.port}` : "";
12-
const database = parsed.pathname.slice(1); // Remove leading slash
11+
const database = parsed.pathname.slice(1);
1312

1413
return `postgresql://***:***@${host}${port}/${database}`;
1514
} catch {
@@ -18,7 +17,41 @@ function redactDatabaseUrl(url: string): string {
1817
}
1918

2019
export default function Home() {
21-
const redactedUrl = redactDatabaseUrl(env.DATABASE_URL);
20+
let databaseUrl = "";
21+
let debugInfo = "";
2222

23-
return <TodoApp databaseUrl={redactedUrl} />;
23+
try {
24+
databaseUrl = env.DATABASE_URL;
25+
debugInfo = "✅ env.DATABASE_URL accessed successfully";
26+
} catch (error) {
27+
debugInfo = `❌ env.DATABASE_URL failed: ${error}`;
28+
// Fallback to raw env
29+
if (process.env.DATABASE_URL) {
30+
databaseUrl = process.env.DATABASE_URL;
31+
debugInfo += " | ✅ process.env.DATABASE_URL fallback worked";
32+
} else {
33+
debugInfo += " | ❌ process.env.DATABASE_URL also empty";
34+
}
35+
}
36+
37+
const redactedUrl = redactDatabaseUrl(databaseUrl);
38+
39+
return (
40+
<div>
41+
<TodoApp databaseUrl={redactedUrl} />
42+
<div
43+
style={{
44+
position: "fixed",
45+
bottom: "10px",
46+
left: "10px",
47+
background: "yellow",
48+
padding: "5px",
49+
fontSize: "12px",
50+
maxWidth: "300px",
51+
}}
52+
>
53+
DEBUG: {debugInfo}
54+
</div>
55+
</div>
56+
);
2457
}

0 commit comments

Comments
 (0)