Improve API error handling to show exact server error message on save failure

This commit is contained in:
AI Bot
2026-07-30 17:56:53 +05:30
parent a327106b19
commit f053f64533
2 changed files with 11 additions and 5 deletions
+9 -3
View File
@@ -58,10 +58,16 @@ export const api = {
const res = await fetch(`${API_URL}/projects`, {
method: 'POST',
headers: getHeaders(),
body: JSON.stringify(projectData)
body: JSON.stringify(projectData),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error);
const text = await res.text();
let data;
try {
data = JSON.parse(text);
} catch(e) {
throw new Error(`Server returned non-JSON (${res.status}): ${text.substring(0, 100)}`);
}
if (!res.ok) throw new Error(data.error || `HTTP ${res.status}`);
return data;
},
+2 -2
View File
@@ -677,9 +677,9 @@ export default function Editor() {
if (projectId === 'new' && result.id) {
navigate(`/editor/${result.id}`, { replace: true });
}
} catch (err) {
} catch (err: any) {
console.error("Failed to save project to cloud", err);
alert("Failed to save project");
alert("Failed to save project: " + (err.message || JSON.stringify(err)));
} finally {
setIsRendering(false);
setOpenMenu(null);