feature/api-doda-update
This commit is contained in:
@@ -552,6 +552,43 @@ async function fetchApiFormDataPost<T = any>(
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Convierte cuerpos de error (JSON o texto) en un mensaje legible para toasts/UX.
|
||||
* Evita mostrar JSON crudo p. ej. `{"error":"HTTP_ERROR","message":"..."}`.
|
||||
*/
|
||||
function messageFromBlobErrorResponse(text: string, status: number): string {
|
||||
const raw = (text || '').trim();
|
||||
if (!raw) {
|
||||
return status === 404
|
||||
? 'No se encontró el recurso. Prueba otro rango o vuelve a intentar.'
|
||||
: `Error ${status} al descargar el archivo.`;
|
||||
}
|
||||
try {
|
||||
const data = JSON.parse(raw) as Record<string, unknown>;
|
||||
if (typeof data.message === 'string' && data.message.trim()) {
|
||||
return data.message.trim();
|
||||
}
|
||||
const d = data.detail;
|
||||
if (typeof d === 'string' && d.trim()) {
|
||||
return d.trim();
|
||||
}
|
||||
if (Array.isArray(d) && d[0] && typeof (d[0] as { msg?: string }).msg === 'string') {
|
||||
return String((d[0] as { msg: string }).msg).trim();
|
||||
}
|
||||
} catch {
|
||||
// no es JSON: usar texto plano si es corto y legible
|
||||
}
|
||||
if (raw.length < 500 && !raw.startsWith('{')) {
|
||||
return raw;
|
||||
}
|
||||
if (raw.startsWith('{')) {
|
||||
return status === 404
|
||||
? 'No se encontró información para exportar. Prueba otras fechas o amplía el rango.'
|
||||
: `Error ${status} al descargar el archivo.`;
|
||||
}
|
||||
return raw;
|
||||
}
|
||||
|
||||
async function fetchBlob(endpoint: string, options: RequestInit = {}): Promise<Blob> {
|
||||
const token = getToken();
|
||||
const headers: Record<string, string> = {
|
||||
@@ -568,9 +605,8 @@ async function fetchBlob(endpoint: string, options: RequestInit = {}): Promise<B
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
// Try to extract some useful message for debugging/UI.
|
||||
const text = await response.text().catch(() => '');
|
||||
throw new Error(text || `Error ${response.status} descargando archivo`);
|
||||
throw new Error(messageFromBlobErrorResponse(text, response.status));
|
||||
}
|
||||
return await response.blob();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user