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"; 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]) { function setOption<T extends OptionNames>(name: T, value: string | OptionDefinitions[T]) {
const option = becca.getOption(name); const option = becca.getOption(name);
@ -137,6 +150,7 @@ export default {
getOption, getOption,
getOptionInt, getOptionInt,
getOptionBool, getOptionBool,
getOptionJson,
setOption, setOption,
createOption, createOption,
getOptions, getOptions,

View File

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

View File

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