fix(media): proper JSON error responses + fix false-positive empty transcript credential error

This commit is contained in:
diegosouzapw
2026-03-15 13:54:33 -03:00
3 changed files with 66 additions and 19 deletions

View File

@@ -24,13 +24,28 @@ import { errorResponse } from "../utils/error.ts";
* Return a CORS error response from an upstream fetch failure
*/
function upstreamErrorResponse(res, errText) {
return new Response(errText, {
status: res.status,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": getCorsOrigin(),
},
});
// Always return JSON so the client can detect 401/credential errors reliably
let errorMessage: string;
try {
const parsed = JSON.parse(errText);
errorMessage =
parsed?.err_msg ||
parsed?.error?.message ||
parsed?.error ||
parsed?.message ||
parsed?.detail ||
errText;
} catch {
errorMessage = errText || `Upstream error (${res.status})`;
}
return Response.json(
{ error: { message: errorMessage, code: res.status } },
{
status: res.status,
headers: { "Access-Control-Allow-Origin": getCorsOrigin() },
}
);
}
/**

View File

@@ -26,13 +26,28 @@ type TranscriptionCredentials = {
* Return a CORS error response from an upstream fetch failure
*/
function upstreamErrorResponse(res, errText) {
return new Response(errText, {
status: res.status,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": getCorsOrigin(),
},
});
// Always return JSON so the client can parse the error reliably
let errorMessage: string;
try {
const parsed = JSON.parse(errText);
errorMessage =
parsed?.err_msg ||
parsed?.error?.message ||
parsed?.error ||
parsed?.message ||
parsed?.detail ||
errText;
} catch {
errorMessage = errText || `Upstream error (${res.status})`;
}
return Response.json(
{ error: { message: errorMessage, code: res.status } },
{
status: res.status,
headers: { "Access-Control-Allow-Origin": getCorsOrigin() },
}
);
}
/**
@@ -71,9 +86,14 @@ async function handleDeepgramTranscription(providerConfig, file, modelId, token)
const data = await res.json();
// Transform Deepgram response to OpenAI Whisper format
const text = data.results?.channels?.[0]?.alternatives?.[0]?.transcript || "";
const text = data.results?.channels?.[0]?.alternatives?.[0]?.transcript ?? null;
return Response.json({ text }, { headers: { "Access-Control-Allow-Origin": getCorsOrigin() } });
// null means the audio had no recognizable speech (music, silence, etc.)
// Return it explicitly so the client can distinguish from a credentials error
return Response.json(
{ text: text ?? "", noSpeechDetected: text === null || text === "" },
{ headers: { "Access-Control-Allow-Origin": getCorsOrigin() } }
);
}
/**

View File

@@ -328,6 +328,7 @@ function getVoiceList(providerId: string) {
function parseApiError(raw: any, statusCode: number): { message: string; isCredentials: boolean } {
const msg =
raw?.error?.message ||
raw?.err_msg ||
raw?.error ||
raw?.message ||
raw?.detail ||
@@ -340,6 +341,7 @@ function parseApiError(raw: any, statusCode: number): { message: string; isCrede
msg.toLowerCase().includes("invalid api key") ||
msg.toLowerCase().includes("unauthorized") ||
msg.toLowerCase().includes("authentication") ||
msg.toLowerCase().includes("api key") ||
statusCode === 401 ||
statusCode === 403);
@@ -519,12 +521,22 @@ export default function MediaPageClient() {
throw new Error(message);
}
const data = await res.json();
// Warn if text is empty (likely missing credentials that returned silently)
// Check for noSpeechDetected flag (music, silence, etc.) — NOT a credential error
if (data?.noSpeechDetected) {
setError(
`No speech detected in the audio file. If you uploaded music or a silent file, try an audio file with spoken words. Provider: "${selectedProvider}".`
);
setIsCredentialsError(false);
setLoading(false);
return;
}
// Warn if text is empty without the noSpeechDetected flag (unexpected)
if (data && typeof data.text === "string" && data.text.trim() === "") {
setError(
`Transcription returned empty text. Make sure you have a valid API key for "${selectedProvider}" configured in /dashboard/providers.`
`Transcription returned empty text. The audio may contain no recognizable speech, or the "${selectedProvider}" API key may be invalid. Check Dashboard → Logs → Proxy for details.`
);
setIsCredentialsError(true);
// Only mark as credential error if we can confirm it from context
setIsCredentialsError(false);
setLoading(false);
return;
}