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`, {
|
||||
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;
|
||||
},
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user