fix: mejorar manejo de errores en funciones de servicio y respuesta de API
This commit is contained in:
@@ -17,4 +17,26 @@ api.interceptors.request.use((config) => {
|
|||||||
return config;
|
return config;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
api.interceptors.response.use(
|
||||||
|
(response) => {
|
||||||
|
return response;
|
||||||
|
},
|
||||||
|
(error) => {
|
||||||
|
if (error.response && error.response.status === 401) {
|
||||||
|
localStorage.removeItem('token');
|
||||||
|
localStorage.removeItem('user');
|
||||||
|
window.location.href = '/login';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error.response && error.response.data) {
|
||||||
|
return Promise.reject({
|
||||||
|
...error,
|
||||||
|
backendData: error.response.data
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.reject(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
export default api;
|
export default api;
|
||||||
@@ -162,51 +162,26 @@ export default function DocEditor() {
|
|||||||
setFinalResult(result);
|
setFinalResult(result);
|
||||||
setStep(3);
|
setStep(3);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error capturado detallado:", error);
|
|
||||||
let friendlyMessage = "Ocurrió un error de validación al generar la gráfica.";
|
|
||||||
|
|
||||||
// 1. BUSCADOR DE ERRORES: Extraemos el detalle venga como venga (Axios, Pydantic, o genérico)
|
let friendlyMessage = "Ocurrió un error al procesar la solicitud.";
|
||||||
let details = error;
|
const errorData = error.backendData || error.response?.data || error;
|
||||||
if (error.response && error.response.data && error.response.data.detail) {
|
|
||||||
details = error.response.data.detail; // Array de Pydantic
|
|
||||||
} else if (error.detail) {
|
|
||||||
details = error.detail;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Si logramos extraer el Array de Pydantic
|
if (errorData.detail) {
|
||||||
if (Array.isArray(details)) {
|
if (errorData.detail === "Invalid input data") {
|
||||||
friendlyMessage = details.map(err => {
|
friendlyMessage = "Revisa los valores del Soporte y Núcleo. Asegúrate de que el 'Inicio del Soporte' sea menor o igual al 'Fin del Soporte', y que el 'Núcleo' esté dentro del 'Soporte'.";
|
||||||
// Limpiamos el texto que ensucia Pydantic ("Value error, ")
|
} else if (typeof errorData.detail === 'string') {
|
||||||
const cleanMsg = err.msg ? err.msg.replace("Value error, ", "") : "Valor incorrecto";
|
friendlyMessage = errorData.detail;
|
||||||
|
|
||||||
// Buscamos en qué etiqueta falló usando err.loc
|
|
||||||
if (err.loc) {
|
|
||||||
const levelIndexPos = err.loc.indexOf("levels");
|
|
||||||
if (levelIndexPos !== -1 && err.loc.length > levelIndexPos + 1) {
|
|
||||||
const levelIndex = err.loc[levelIndexPos + 1];
|
|
||||||
const termName = scaleKeys[levelIndex] || `Nivel ${Number(levelIndex) + 1}`;
|
|
||||||
return `• En la etiqueta "${termName}": ${cleanMsg}`;
|
|
||||||
}
|
|
||||||
// Formato alternativo de Pydantic v2
|
|
||||||
if (typeof err.loc[1] === 'number') {
|
|
||||||
const termName = scaleKeys[err.loc[1]] || `Nivel ${err.loc[1] + 1}`;
|
|
||||||
return `• En la etiqueta "${termName}": ${cleanMsg}`;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return `• ${cleanMsg}`;
|
} else if (errorData.errors && Array.isArray(errorData.errors) && errorData.errors.length > 0) {
|
||||||
}).join("\n");
|
friendlyMessage = errorData.errors.map(err => {
|
||||||
}
|
let cleanMsg = err.msg ? err.msg.replace("Value error, ", "") : "Valor incorrecto";
|
||||||
// 3. Si el backend nos mandó un simple string
|
if (err.loc && err.loc.includes("levels")) {
|
||||||
else if (typeof details === 'string') {
|
const levelIndex = err.loc[err.loc.indexOf("levels") + 1];
|
||||||
friendlyMessage = details;
|
const termName = scaleKeys[levelIndex] || `Nivel ${Number(levelIndex) + 1}`;
|
||||||
}
|
return `• En la etiqueta "${termName}": ${cleanMsg}`;
|
||||||
// 4. Si es un error genérico (Axios de bajo nivel)
|
}
|
||||||
else if (error.message) {
|
return `• ${cleanMsg}`;
|
||||||
if (error.message.includes("422")) {
|
}).join("\n");
|
||||||
friendlyMessage = "Revisa los datos: asegúrate de que el 'Núcleo' esté dentro del 'Soporte' y que c <= d.";
|
|
||||||
} else {
|
|
||||||
friendlyMessage = error.message;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setSubmitError(friendlyMessage);
|
setSubmitError(friendlyMessage);
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ export const calculateValueFunction = async (payload) => {
|
|||||||
const response = await api.post('/criteria/doc/value-function', payload);
|
const response = await api.post('/criteria/doc/value-function', payload);
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error calculating value function:', error);
|
if (error.response && error.response.data) throw error.response.data;
|
||||||
throw error.response?.data?.detail || error.message;
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -15,8 +15,8 @@ export const buildFuzzyGraph = async (payload) => {
|
|||||||
const response = await api.post('/criteria/doc-it2mf/build', payload);
|
const response = await api.post('/criteria/doc-it2mf/build', payload);
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error building fuzzy graph:', error);
|
if (error.response && error.response.data) throw error.response.data;
|
||||||
throw error.response?.data?.detail || error.message;
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -25,8 +25,8 @@ export const saveToHistory = async (payload) => {
|
|||||||
const response = await api.post('/history/add', payload);
|
const response = await api.post('/history/add', payload);
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error saving to history:', error);
|
if (error.response && error.response.data) throw error.response.data;
|
||||||
throw error.response?.data?.detail || error.message;
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -35,17 +35,17 @@ export const getUserHistory = async () => {
|
|||||||
const response = await api.get('/history/user');
|
const response = await api.get('/history/user');
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching history:', error);
|
if (error.response && error.response.data) throw error.response.data;
|
||||||
throw error.response?.data?.detail || error.message;
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const deleteHistoryItem = async (id) => {
|
export const deleteHistoryItem = async (id) => {
|
||||||
try {
|
try {
|
||||||
const response = await api.delete(`/history/delete/${id}`);
|
const response = await api.delete(`/history/${id}`);
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error deleting history item:', error);
|
if (error.response && error.response.data) throw error.response.data;
|
||||||
throw error.response?.data?.detail || error.message;
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user