Fehlerhandling von Errorcodes

response und das json werden jeweils gespeichert, funktion bricht ab
wenn statuscode nicht übereinstimmt (response.status != xxx zu
!response.ok mass-refactored)
This commit is contained in:
Technoir committed 2026-10-11 14:33:14 +02:00
1 parent a49506fece
commit a7d467f187
2 files changed
+94 -32

No files matched your search

@@ -5,9 +5,15 @@ import * as ui from "./frontend/indexUI.js"
const addListForm = document.querySelector(".addListForm");
async function addList(listName) {
const listAsJson = await api.addList(listName);
console.log(`${listAsJson}`);
if (listAsJson) ui.addList(listAsJson);
const response = await api.addList(listName);
const json = await response.json();
if (!response.ok) {
console.log(`error creating list ${json}`);
return;
}
ui.addList(json);
}
function editList(listLi) {
@@ -19,13 +25,19 @@ function editList(listLi) {
formElement.querySelector(".editForm__name").focus();
function save() {
async function save() {
let formData = Object.fromEntries(new FormData(formElement));
ui.editList(id, formElement, formData, listLi);
console.log(`editing catogory ${id} with ${formData}`)
api.editList(id, { newListName: formData.newListName });
const response = await api.editList(id, { newListName: formData.newListName });
if (!response.ok) {
const json = await response.json();
console.log(`error editing list ${json}`);
return;
}
ui.editList(id, formElement, formData, listLi);
}
formElement.addEventListener("submit", (event) => {
@@ -44,7 +56,13 @@ async function deleteList(listElement) {
const response = await api.deleteList(listElement.dataset.id);
if (response.status == 204) ui.deleteList(listElement);
if (!response.ok) {
const json = await response.json();
console.log(`error deleting list ${json}`);
return;
}
ui.deleteList(listElement);
}
+69 -25
View File
@@ -9,16 +9,31 @@ const deleteAllDoneItemsButton = document.querySelector(".deleteAllDoneItemsButt
async function addItem(itemName, itemAmount, categoryID) {
console.log(` adding item with id:${currentList.id}, name: ${itemName}, amount: ${itemAmount}, category: ${categoryID}`)
const itemAsJSON = await api.addItem(currentList.id, itemName, itemAmount, categoryID);
console.log(`rendering frointend item: ${itemAsJSON}`);
ui.addItem(itemAsJSON);
const response = await api.addItem(currentList.id, itemName, itemAmount, categoryID);
const json = await response.json();
if (!response.ok) {
console.log(`Error, item couldnt be created ${json}`);
return;
}
console.log(`rendering frontend item: ${json}`);
ui.addItem(json);
}
async function addCategory(categoryName) {
console.log(`adding category ${categoryName}`)
const categoryAsJSON = await api.addCategory(currentList.id, categoryName);
console.log(categoryAsJSON);
ui.addCategory(categoryAsJSON);
const response = await api.addCategory(currentList.id, categoryName);
const json = await response.json();
if (!response.ok) {
console.log(`Error, category couldnt be created ${json}`)
return;
}
ui.addCategory(json);
return 0;
}
@@ -34,18 +49,20 @@ async function editItem(textElement) {
formElement.querySelector(".editForm__name").focus();
function save() {
async function save() {
let formData = Object.fromEntries(new FormData(formElement));
let newName = formData.newItemName;
let newAmount = formData.newItemAmount;
textElement.querySelector(".item__textName").textContent = newName;
textElement.querySelector(".item__textAmount").textContent = newAmount;
formElement.replaceWith(textElement);
console.log(`editing item ${id} with ${formData}`)
api.editItem(id, { itemName: newName, itemAmount: newAmount });
const response = await api.editItem(id, { itemName: formData.newName, itemAmount: formData.newAmount });
if (!response.ok) {
const json = await response.json();
console.log(`Error editingItem ${json}`);
return;
}
ui.editItem(textElement, formElement, formData);
}
formElement.addEventListener("submit", (event) => {
@@ -60,7 +77,7 @@ async function editItem(textElement) {
}
async function editCategory(categoryElement) {
function editCategory(categoryElement) {
//erstellen das Forms
//wir nutzen für das Element nur das div, weil sonst alle items mitgelöscht werden würden
const categoryDiv = categoryElement.querySelector(".category__div");
@@ -71,13 +88,21 @@ async function editCategory(categoryElement) {
formElement.querySelector(".editForm__name").focus();
function save() {
async function save() {
let formData = Object.fromEntries(new FormData(formElement));
ui.editCategory(id, formElement, formData, categoryDiv);
console.log(`editing catogory ${id} with ${formData}`)
api.editCategory(id, { categoryName: formData.newCategoryName });
const response = await api.editCategory(id, { categoryName: formData.newCategoryName });
if (!response.ok) {
const json = await response.json();
console.log(`Error editing category ${json}`);
return;
}
ui.editCategory(id, formElement, formData, categoryDiv);
}
formElement.addEventListener("submit", (event) => {
@@ -90,20 +115,39 @@ async function editCategory(categoryElement) {
})
}
function deleteItem(li) {
api.deleteItem(li.id);
async function deleteItem(li) {
const response = await api.deleteItem(li.id);
if (!response.ok) {
const json = await response.json();
console.log(`Error deleting item ${json}`);
return;
}
ui.deleteItem(li);
}
function deleteCategory(deleteAllItems, categoryID) {
async function deleteCategory(deleteAllItems, categoryID) {
console.log(`deleting category with allItems: ${deleteAllItems} id: ${categoryID}`);
api.deleteCategory(deleteAllItems, categoryID);
const response = await api.deleteCategory(deleteAllItems, categoryID);
if (!response.ok) {
const json = await response.json();
console.log(`Error deleting category ${json}`);
return;
}
ui.deleteCategory(deleteAllItems, categoryID);
}
function deleteAllDoneItems() {
async function deleteAllDoneItems() {
console.log(`deleting all done items...`)
api.deleteAllDoneItems(currentList.id);
const response = await api.deleteAllDoneItems(currentList.id);
if (!response.ok) {
const json = await response.json();
console.log(`Error deleting all done items ${json}`);
return;
}
ui.deleteAllDoneItems();
}