Skip to content

Commit 51519e2

Browse files
authored
fix: handle undefined arguments in executeJavaScript call (#1958)
* fix: handle undefined arguments in executeJavaScript call * fix: improve error handling in setVoice and getVoices procedures * fix: handle undefined arguments in executeJavaScript call
1 parent 22e2c0d commit 51519e2

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

apps/main/src/tipc/reader.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,34 @@ export const readerRoute = {
5151
try {
5252
const voices = await tts.getVoices()
5353
return voices
54-
} catch (error: any) {
55-
if (!window) {
54+
} catch (error) {
55+
console.error("Failed to get voices", error)
56+
if (!window) return
57+
if (error instanceof Error) {
58+
void callWindowExpose(window).toast.error(error.message, { duration: 1000 })
5659
return
5760
}
58-
void callWindowExpose(window).toast.error(error.message, {
59-
duration: 1000,
60-
})
61+
callWindowExpose(window).toast.error("Failed to get voices", { duration: 1000 })
6162
}
6263
}),
6364

6465
setVoice: t.procedure.input<string>().action(async ({ input, context: { sender } }) => {
6566
const window = BrowserWindow.fromWebContents(sender)
66-
if (!window) {
67-
return
68-
}
69-
70-
await tts.setMetadata(input, OUTPUT_FORMAT.WEBM_24KHZ_16BIT_MONO_OPUS).catch((error) => {
71-
const msg = typeof error === "string" ? error : error.message
72-
return callWindowExpose(window).toast.error(msg || "unknown set voice error", {
73-
duration: 1000,
67+
if (!window) return
68+
69+
await tts
70+
.setMetadata(input, OUTPUT_FORMAT.WEBM_24KHZ_16BIT_MONO_OPUS)
71+
.catch((error: unknown) => {
72+
console.error("Failed to set voice", error)
73+
if (error instanceof Error) {
74+
return callWindowExpose(window).toast.error(error.message, {
75+
duration: 1000,
76+
})
77+
}
78+
return callWindowExpose(window).toast.error("Failed to set voice", {
79+
duration: 1000,
80+
})
7481
})
75-
})
7682
}),
7783

7884
detectCodeStringLanguage: t.procedure

packages/shared/src/bridge.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ function createProxy<T extends RenderGlobalContext>(window: BrowserWindow, path:
8989

9090
try {
9191
return await window.webContents.executeJavaScript(
92-
`(async () => { try { return await globalThis.${PREFIX}?.${methodPath}?.(${args.map((arg) => JSON.stringify(arg)).join(",")}) } catch (err) { console.error('Failed to executeJavaScript: ${methodPath}', err) } })()`,
92+
`globalThis.${PREFIX}?.${methodPath}?.(${args
93+
.map((arg) => {
94+
if (arg === undefined) return "undefined"
95+
return JSON.stringify(arg)
96+
})
97+
.join(",")})`,
9398
)
9499
} catch (err) {
95100
console.error(`Failed to executeJavaScript: ${methodPath}`, err)

0 commit comments

Comments
 (0)