feature/rbac y perfiles implementados
This commit is contained in:
19
src/api/apiError.js
Normal file
19
src/api/apiError.js
Normal file
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* 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}`;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user