Refactor error handling and add validation for adventure name in server and page files

This commit is contained in:
Sean Morley
2024-04-28 21:54:43 +00:00
parent d84c9f4d24
commit 88a2ac07e6
2 changed files with 27 additions and 20 deletions

View File

@@ -78,7 +78,16 @@
newAdventure,
}),
})
.then((response) => response.json())
.then((response) => {
if (!response.ok) {
return response.json().then((data) => {
throw new Error(
data.error || `Failed to add adventure - ${data?.message}`
);
});
}
return response.json();
})
.then((data) => {
let newId = data.id;
// add to local array for instant view update
@@ -100,6 +109,7 @@
})
.catch((error) => {
console.error("Error:", error);
showToast(error.message);
});
};