chore(window): handle potential JSON parsing failures

This commit is contained in:
SiriusXT 2025-12-29 16:26:50 +08:00
parent c28f11336e
commit 254145f0e5
3 changed files with 16 additions and 4 deletions

View File

@ -72,6 +72,19 @@ function getOptionBool(name: FilterOptionsByType<boolean>): boolean {
return val === "true";
}
function getOptionJson(name: OptionNames) {
const val = getOptionOrNull(name);
if (typeof val !== "string") {
return null;
}
try {
return JSON.parse(val);
} catch (e) {
return null;
}
}
function setOption<T extends OptionNames>(name: T, value: string | OptionDefinitions[T]) {
const option = becca.getOption(name);
@ -137,6 +150,7 @@ export default {
getOption,
getOptionInt,
getOptionBool,
getOptionJson,
setOption,
createOption,
getOptions,

View File

@ -197,7 +197,7 @@ function updateTrayMenu() {
}
function buildClosedWindowsMenu() {
const savedOpenNoteContexts = JSON.parse(optionService.getOption("openNoteContexts") || "[]");
const savedOpenNoteContexts = optionService.getOptionJson("openNoteContexts") || "[]";
const openedWindowIds = windowService.getAllWindowIds();
const closedWindows = savedOpenNoteContexts
.filter(win => !openedWindowIds.includes(win.windowId))

View File

@ -37,9 +37,7 @@ function trackWindowFocus(win: BrowserWindow, windowId: string) {
win.on("closed", () => {
cls.wrap(() => {
const savedWindows = JSON.parse(
optionService.getOption("openNoteContexts") || "[]"
);
const savedWindows = optionService.getOptionJson("openNoteContexts") || "[]";
const win = savedWindows.find(w => w.windowId === windowId);
if (win) {