refactor(#56): Improve how the dex updating is handled and the error messaging around it

This commit is contained in:
Josh Creek
2026-01-10 12:07:05 +00:00
parent 845cc1c739
commit b1b6e63b6f
11 changed files with 700 additions and 107 deletions
+20 -4
View File
@@ -38,6 +38,22 @@
}
}
async function readApiErrorMessage(response: Response): Promise<string> {
try {
const data = await response.json();
if (data && typeof data === 'object' && 'error' in data && typeof data.error === 'string') {
return data.error;
}
} catch {
// fall through
}
try {
return await response.text();
} catch {
return 'Request failed';
}
}
function openCreateModal() {
editingPokedex = null;
formData = {
@@ -75,9 +91,9 @@
});
if (!response.ok) {
const errorText = await response.text();
const errorText = await readApiErrorMessage(response);
console.error('Failed to update pokédex:', response.status, errorText);
alert(`Failed to update pokédex: ${errorText}`);
alert(errorText);
return;
}
@@ -94,9 +110,9 @@
});
if (!response.ok) {
const errorText = await response.text();
const errorText = await readApiErrorMessage(response);
console.error('Failed to create pokédex:', response.status, errorText);
alert(`Failed to create pokédex: ${errorText}`);
alert(errorText);
return;
}
const createdPokedex = (await response.json()) as Pokedex;