Skip to content

Commit

Permalink
Enhance error handling in handleError.js to support various error for…
Browse files Browse the repository at this point in the history
…mats
  • Loading branch information
S-e-b-a-s committed Nov 5, 2024
1 parent 27d1c78 commit ef629aa
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions frontend/src/assets/handleError.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,31 @@ export const handleError = async (response, showSnack) => {
switch (response.status) {
case 400:
const data = await response.json();
const firstKey = Object.keys(data)[0];
errorMessage = firstKey
? data[firstKey]
: 'Por favor, verifica la información ingresada y vuelve a intentarlo.';
let firstKey = Object.keys(data)[0];

if (firstKey === 'Error') {
// Check if the "Error" value is a string and handle it directly
if (typeof data[firstKey] === 'string') {
errorMessage = data[firstKey]; // Directly set the error message
} else if (Array.isArray(data[firstKey])) {
// If it's an array, pick the first item
errorMessage = data[firstKey][0];
} else {
// If it's an object or anything unexpected, fall back to a generic message
errorMessage =
'Por favor, verifica la información ingresada y vuelve a intentarlo.';
}
} else {
// Handle non-nested error cases (same logic as before)
if (typeof data[firstKey] === 'string') {
errorMessage = data[firstKey];
} else if (Array.isArray(data[firstKey])) {
errorMessage = data[firstKey][0];
} else {
errorMessage =
'Por favor, verifica la información ingresada y vuelve a intentarlo.';
}
}
break;
case 401:
errorMessage =
Expand Down

0 comments on commit ef629aa

Please sign in to comment.