Skip to content

fix: health endpoint crashes when Supabase env vars are missing (#36)#41

Closed
zacharias-ona wants to merge 1 commit into
mainfrom
fix/36-health-env-guard
Closed

fix: health endpoint crashes when Supabase env vars are missing (#36)#41
zacharias-ona wants to merge 1 commit into
mainfrom
fix/36-health-env-guard

Conversation

@zacharias-ona
Copy link
Copy Markdown
Collaborator

Closes #36

What

The health endpoint (GET /api/health) reported {"status":"down","db":{"connected":false,"latency_ms":0}} when Supabase environment variables were not configured in Vercel. The route handler called createClient() without checking whether NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY were set. The non-null assertions (!) silently passed undefined to createServerClient, which threw when making the HTTP request. The catch block then reported the database as "down" — misleading, since the database was never reached.

How

Added an env var guard at the top of the health route handler, matching the pattern already used in src/proxy.ts. When Supabase env vars are missing, the endpoint returns {"status":"ok","db":{"connected":false,"latency_ms":0,"reason":"not_configured"}} instead of crashing into the catch block. The overall app status remains "ok" since the app itself is running — only the DB connection is absent.

Also updated .agents/conventions.md with the env var guard pattern (placed next to the API routes section) to prevent the same class of bug in future route handlers.

Testing

Added 6 regression tests in src/app/api/health/route.test.ts:

  • Missing both env vars → returns not_configured (would have caught this bug)
  • Missing only URL → returns not_configured
  • Table does not exist → returns ok/connected (connection worked)
  • Successful query → returns ok/connected
  • Non-connection error (e.g., permission denied) → returns degraded
  • Client throws (e.g., fetch failed) → returns down

Run: pnpm test -- src/app/api/health/route.test.ts

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 15, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
memo Ready Ready Preview, Comment Apr 15, 2026 10:00am

Request Review

@zacharias-ona
Copy link
Copy Markdown
Collaborator Author

Closing as duplicate. The identical fix was already merged via PR #40 (commit 8e30ff2 on main, merged 2026-04-15T10:00:32Z). This PR has merge conflicts because the same changes already exist on main.

@zacharias-ona
Copy link
Copy Markdown
Collaborator Author

Review — no blocking issues found.

  • Correctness: Env var guard prevents createClient() from receiving undefined, returns status: ok with reason: not_configured — appropriate since the app is running, only DB is absent.
  • Tests: 6 tests cover all branches (missing env vars, partial, table-not-found, success, degraded, client throw). Assertions match the route handler.
  • Conventions: Follows the proxy pattern, strict TypeScript, named export, no suppressions.
  • Scope: Limited to health route, its tests, and conventions docs.

@zacharias-ona
Copy link
Copy Markdown
Collaborator Author

Closing in favor of #43, which applies the same changes rebased cleanly onto current main. This PR had unresolvable merge conflicts due to overlapping changes from #40 already on main.

zacharias-ona added a commit that referenced this pull request Apr 15, 2026
The PULL_REQUEST_EVENT_MERGED webhook and */10 cron can fire
simultaneously, spawning two agents that both pick the same issue
before either marks it in-progress. This caused duplicate PRs for
issues #26 (PRs #45/#46) and #36 (PRs #40/#41).

The cron alone ensures work is picked up within 10 minutes.

Co-authored-by: Ona <no-reply@ona.com>
zacharias-ona added a commit that referenced this pull request Apr 15, 2026
The PULL_REQUEST_EVENT_MERGED webhook and */10 cron can fire
simultaneously, spawning two agents that both pick the same issue
before either marks it in-progress. This caused duplicate PRs for
issues #26 (PRs #45/#46) and #36 (PRs #40/#41).

The cron alone ensures work is picked up within 10 minutes.

Co-authored-by: Ona <no-reply@ona.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: UI regression after PR #34 — Health endpoint reports database down

1 participant