Improve API error handling to show exact server error message on save failure
This commit is contained in:
+9
-3
@@ -58,10 +58,16 @@ export const api = {
|
|||||||
const res = await fetch(`${API_URL}/projects`, {
|
const res = await fetch(`${API_URL}/projects`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: getHeaders(),
|
headers: getHeaders(),
|
||||||
body: JSON.stringify(projectData)
|
body: JSON.stringify(projectData),
|
||||||
});
|
});
|
||||||
const data = await res.json();
|
const text = await res.text();
|
||||||
if (!res.ok) throw new Error(data.error);
|
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;
|
return data;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -677,9 +677,9 @@ export default function Editor() {
|
|||||||
if (projectId === 'new' && result.id) {
|
if (projectId === 'new' && result.id) {
|
||||||
navigate(`/editor/${result.id}`, { replace: true });
|
navigate(`/editor/${result.id}`, { replace: true });
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err: any) {
|
||||||
console.error("Failed to save project to cloud", err);
|
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 {
|
} finally {
|
||||||
setIsRendering(false);
|
setIsRendering(false);
|
||||||
setOpenMenu(null);
|
setOpenMenu(null);
|
||||||
|
|||||||
Reference in New Issue
Block a user