chg: frontend errorFormating

This commit is contained in:
Alex
2024-10-09 18:05:27 +02:00
parent 451e42a1fc
commit c008bcad0b

View File

@@ -68,38 +68,33 @@ export function isEmpty(obj) {
} }
return true; return true;
} }
/**
* Test whether or not an object is empty.
* @param {any} obj - The object to test
* @returns `true` or `false`
*/
export function formatError(obj) { export function formatError(obj) {
const errors = []; const errors = [];
if (typeof obj === "object" && obj !== null) { if (typeof obj === "object" && obj !== null) {
if (Array.isArray(obj)) { if (Array.isArray(obj)) {
obj.forEach((/** @type {Object} */ error) => { obj.forEach((error) => {
Object.keys(error).map((k) => { errors.push({
errors.push({ field: error.field,
error: error[k], key: error.key,
id: Math.random() * 1000, id: Math.random() * 1000,
});
}); });
}); });
} else { } else {
Object.keys(obj).map((k) => { Object.keys(obj).forEach((field) => {
errors.push({ errors.push({
error: obj[k], field: field,
key: obj[field].key,
id: Math.random() * 1000, id: Math.random() * 1000,
}); });
}); });
} }
} else { } else {
errors.push({ errors.push({
error: obj.charAt(0).toUpperCase() + obj.slice(1), field: "general",
key: obj,
id: 0, id: 0,
}); });
} }
return errors; return errors;
} }