Skip to content

Commit 6ce0640

Browse files
committed
fix: improve elevenlabs 404 proxy error handling
1 parent 69db955 commit 6ce0640

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

netlify/functions/elevenlabs-signed-url.mts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,19 @@ export default async (req: Request, _context: Context) => {
5151
if (!response.ok) {
5252
const errorText = await response.text();
5353
console.error("ElevenLabs signed URL error:", response.status, errorText);
54+
// Return 502 (Bad Gateway) instead of passing through ElevenLabs' status code.
55+
// This prevents a 404 from ElevenLabs (e.g. invalid agent ID) from looking
56+
// like the Netlify function itself is missing.
57+
const proxyStatus = response.status >= 400 && response.status < 500 ? 502 : response.status;
5458
return new Response(
5559
JSON.stringify({
56-
error: `ElevenLabs error: ${response.status}`,
60+
error: `ElevenLabs upstream error (HTTP ${response.status})`,
5761
details: errorText,
62+
hint: response.status === 404
63+
? "The ElevenLabs agent ID may be invalid or deleted. Check ELEVENLABS_AGENT_ID env var."
64+
: undefined,
5865
}),
59-
{ status: response.status, headers: { "Content-Type": "application/json" } }
66+
{ status: proxyStatus, headers: { "Content-Type": "application/json" } }
6067
);
6168
}
6269

src/services/elevenlabsSignedUrl.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ export async function fetchElevenLabsSignedUrl(): Promise<string> {
6363
});
6464

6565
if (response.status === 404) {
66+
// Check if the 404 body contains an actual upstream error (old behavior)
67+
// vs. a genuine "endpoint not found".
68+
const body = await response.text().catch(() => '');
69+
if (body.includes('ElevenLabs') || body.includes('upstream')) {
70+
// This is an upstream error proxied through our function — don't try more endpoints
71+
throw new Error(`${endpoint}: ${body}`);
72+
}
6673
errors.push(`${endpoint}: HTTP 404`);
6774
continue;
6875
}

0 commit comments

Comments
 (0)