Skip to content

Commit dd2ebb7

Browse files
committed
fix(demo/ctx): add error handling for readFile, readFileSync, and JSON.parse in voices
Made-with: Cursor
1 parent 7603f2a commit dd2ebb7

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

demo/ctx/src/voices.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,26 @@ export async function runVoices(
6565
}
6666

6767
const context = readFile(contextPath);
68-
const i18nRaw = fs.readFileSync(i18nPath, "utf-8");
69-
const i18n = JSON.parse(i18nRaw);
68+
if (context.startsWith("[Error:")) {
69+
fail(`Cannot read context file: ${contextPath}`);
70+
return;
71+
}
72+
73+
let i18nRaw: string;
74+
try {
75+
i18nRaw = fs.readFileSync(i18nPath, "utf-8");
76+
} catch (e) {
77+
fail(`Cannot read i18n file: ${i18nPath}\n${e}`);
78+
return;
79+
}
80+
81+
let i18n: Record<string, unknown>;
82+
try {
83+
i18n = JSON.parse(i18nRaw);
84+
} catch (e) {
85+
fail(`Malformed JSON in ${i18nPath}: ${e}`);
86+
return;
87+
}
7088
const voices: Record<string, string> = { ...(i18n.provider?.voices ?? {}) };
7189

7290
phase("Brand Voices", targetLocales.join(" "));

0 commit comments

Comments
 (0)