fix(beacon): surface Groq STT errors with actionable status codes + hints

Investigated the user's 'STT microphone not working properly' report.
Found two real problems on this machine:

  1. GROQ_API_KEY in .env.local returns 401 from Groq (direct probe of
     https://api.groq.com/openai/v1/audio/transcriptions with the same
     bearer returned {'error':{'message':'Invalid API Key'}}). User
     needs to rotate the key at https://console.groq.com.

  2. Local Whisper 'base' fallback takes 38-45s per 1-second WAV —
     within the route's 60s timeout but well past any user's patience.

The route's 401 path previously wrapped the failure as a generic HTTP
500 with the error body inlined as a string — easy to miss in a network
tab, no actionable hint. Now classifies common Groq failure modes:

  • 401 (Invalid API Key) → HTTP 502 with 'rotate the key' hint
  • 429 (rate limited)    → HTTP 429 with 'retry or switch to local'
  • timeout / abort       → HTTP 504 with 'try a shorter recording'
  • other                 → HTTP 502 (was 500; bad-upstream is more
                              honest — the route itself didn't fail)

The original error is preserved as  for debugging without
poisoning the user-facing  string. Local Whisper path
unchanged — the slowness there is a separate question (model size +
GPU vs CPU) and outside the scope of error-message work.

tsc clean, eslint clean. Doesn't fix the user's 401 — they still need
to rotate the Groq key — but ensures future occurrences are diagnosed
in seconds instead of requiring a deep dive into the route source.
This commit is contained in:
g-but committed 2026-05-20 08:21:14 +02:00
1 parent b323e74357
commit 92a30e69c0
1 file changed
+23 -1
+23 -1
View File
@@ -64,8 +64,30 @@ export async function POST(req: NextRequest) {
if (!text) return NextResponse.json({ error: "No speech detected" }, { status: 422 });
return NextResponse.json({ text });
} catch (err) {
// Surface auth/quota problems with actionable status codes + hints so a
// user seeing "STT not working" sees the real cause in the network tab
// instead of a generic 500. callGroqTranscribe throws messages shaped
// "groq transcribe 401: <body>" — match on the status segment.
const msg = err instanceof Error ? err.message : String(err);
return NextResponse.json({ error: msg }, { status: 500 });
if (/groq transcribe 401/i.test(msg) || /invalid.*api.*key/i.test(msg)) {
return NextResponse.json({
error: "Groq API key invalid — rotate it at https://console.groq.com and update GROQ_API_KEY in your env.",
detail: msg,
}, { status: 502 });
}
if (/groq transcribe 429/i.test(msg) || /rate.?limit/i.test(msg)) {
return NextResponse.json({
error: "Groq rate-limited this transcription — retry in a moment or switch to local transcription.",
detail: msg,
}, { status: 429 });
}
if (/abort|timeout/i.test(msg)) {
return NextResponse.json({
error: "Groq transcription timed out (>30s) — try a shorter recording.",
detail: msg,
}, { status: 504 });
}
return NextResponse.json({ error: msg }, { status: 502 });
}
}