Skip to content

Commit af60793

Browse files
Audatic07coderabbitai[bot]Nihal4777
authored
Fix: added graceful error handling on recover project (#931)
* fix: added graceful error handling on recover project * chore: added comment to rerun ci * fix: added storage clear to load failure too Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * fix: coderabbit suggested change Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * fix: formatting errors resolved * fix: confirm option in its own try catch * fix: try catch flattened, also, confirm option failure preservation of data reflected in v1 *chore: sync v0 version with v1 --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Nihal <65150640+Nihal4777@users.noreply.github.com>
1 parent ed7e693 commit af60793

File tree

2 files changed

+102
-16
lines changed

2 files changed

+102
-16
lines changed

src/simulator/src/data/project.ts

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,58 @@ import { confirmOption } from "#/components/helpers/confirmComponent/ConfirmComp
1616
* @category data
1717
*/
1818
export async function recoverProject() {
19-
if (localStorage.getItem("recover")) {
20-
const recover = localStorage.getItem("recover");
21-
const data = recover ? JSON.parse(recover) : {};
22-
if (await confirmOption(`Would you like to recover: ${data.name}`)) {
23-
load(data);
24-
}
25-
localStorage.removeItem("recover");
26-
} else {
19+
const recoverData = localStorage.getItem("recover");
20+
21+
if (!recoverData) {
2722
showError("No recover project found");
23+
return;
24+
}
25+
26+
let data: any;
27+
try {
28+
data = JSON.parse(recoverData);
29+
} catch (parseError) {
30+
showError("Recovery data is corrupted and cannot be parsed");
31+
localStorage.removeItem("recover");
32+
console.error("Parse error:", parseError);
33+
return;
34+
}
35+
36+
if (!data || typeof data !== "object") {
37+
showError("Recovery data is invalid");
38+
localStorage.removeItem("recover");
39+
return;
40+
}
41+
42+
if (!data.scopes || !Array.isArray(data.scopes) || data.scopes.length === 0) {
43+
showError("Recovery data contains no valid circuits");
44+
localStorage.removeItem("recover");
45+
return;
46+
}
47+
48+
const projectName = data.name || "Untitled";
49+
50+
let confirmed: boolean;
51+
try {
52+
confirmed = await confirmOption(`Would you like to recover: ${projectName}?`);
53+
} catch (confirmError) {
54+
showError("Recovery confirmation failed");
55+
console.error("Confirm error:", confirmError);
56+
return;
57+
}
58+
59+
if (!confirmed) {
60+
// Keep recovery data so the user can retry via Project → Recover Project
61+
return;
62+
}
63+
64+
try {
65+
load(data);
66+
showMessage(`Project "${projectName}" recovered successfully`);
67+
localStorage.removeItem("recover");
68+
} catch (loadError) {
69+
showError("Failed to load recovered project");
70+
console.error("Load error:", loadError);
2871
}
2972
}
3073

v1/src/simulator/src/data/project.ts

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,58 @@ import { confirmOption } from "#/components/helpers/confirmComponent/ConfirmComp
1616
* @category data
1717
*/
1818
export async function recoverProject() {
19-
if (localStorage.getItem("recover")) {
20-
const recover = localStorage.getItem("recover");
21-
const data = recover ? JSON.parse(recover) : {};
22-
if (await confirmOption(`Would you like to recover: ${data.name}`)) {
23-
load(data);
24-
}
25-
localStorage.removeItem("recover");
26-
} else {
19+
const recoverData = localStorage.getItem("recover");
20+
21+
if (!recoverData) {
2722
showError("No recover project found");
23+
return;
24+
}
25+
26+
let data: any;
27+
try {
28+
data = JSON.parse(recoverData);
29+
} catch (parseError) {
30+
showError("Recovery data is corrupted and cannot be parsed");
31+
localStorage.removeItem("recover");
32+
console.error("Parse error:", parseError);
33+
return;
34+
}
35+
36+
if (!data || typeof data !== "object") {
37+
showError("Recovery data is invalid");
38+
localStorage.removeItem("recover");
39+
return;
40+
}
41+
42+
if (!data.scopes || !Array.isArray(data.scopes) || data.scopes.length === 0) {
43+
showError("Recovery data contains no valid circuits");
44+
localStorage.removeItem("recover");
45+
return;
46+
}
47+
48+
const projectName = data.name || "Untitled";
49+
50+
let confirmed: boolean;
51+
try {
52+
confirmed = await confirmOption(`Would you like to recover: ${projectName}?`);
53+
} catch (confirmError) {
54+
showError("Recovery confirmation failed");
55+
console.error("Confirm error:", confirmError);
56+
return;
57+
}
58+
59+
if (!confirmed) {
60+
// Keep recovery data so the user can retry via Project → Recover Project
61+
return;
62+
}
63+
64+
try {
65+
load(data);
66+
showMessage(`Project "${projectName}" recovered successfully`);
67+
localStorage.removeItem("recover");
68+
} catch (loadError) {
69+
showError("Failed to load recovered project");
70+
console.error("Load error:", loadError);
2871
}
2972
}
3073

0 commit comments

Comments
 (0)