Files
frontend/src/api/apiError.js

20 lines
686 B
JavaScript

/**
* Extrae el mensaje de error de una respuesta HTTP fallida.
* Lee el JSON del body y devuelve `detail`, `message` o `error` del backend.
* Si no hay JSON, devuelve el texto o un fallback genérico con el status.
*/
export async function extractApiError(response) {
const status = response.status;
try {
const contentType = response.headers.get('content-type') || '';
if (contentType.includes('application/json')) {
const body = await response.json();
return body.detail || body.message || body.error || `Error ${status}`;
}
const text = await response.text();
return text || `Error ${status}`;
} catch {
return `Error ${status}`;
}
}