Skip to content

Commit

Permalink
fix: handle undefined arguments in executeJavaScript call (#1958)
Browse files Browse the repository at this point in the history
* fix: handle undefined arguments in executeJavaScript call

* fix: improve error handling in setVoice and getVoices procedures

* fix: handle undefined arguments in executeJavaScript call
  • Loading branch information
lawvs authored Dec 2, 2024
1 parent 22e2c0d commit 51519e2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
34 changes: 20 additions & 14 deletions apps/main/src/tipc/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,34 @@ export const readerRoute = {
try {
const voices = await tts.getVoices()
return voices
} catch (error: any) {
if (!window) {
} catch (error) {
console.error("Failed to get voices", error)
if (!window) return
if (error instanceof Error) {
void callWindowExpose(window).toast.error(error.message, { duration: 1000 })
return
}
void callWindowExpose(window).toast.error(error.message, {
duration: 1000,
})
callWindowExpose(window).toast.error("Failed to get voices", { duration: 1000 })
}
}),

setVoice: t.procedure.input<string>().action(async ({ input, context: { sender } }) => {
const window = BrowserWindow.fromWebContents(sender)
if (!window) {
return
}

await tts.setMetadata(input, OUTPUT_FORMAT.WEBM_24KHZ_16BIT_MONO_OPUS).catch((error) => {
const msg = typeof error === "string" ? error : error.message
return callWindowExpose(window).toast.error(msg || "unknown set voice error", {
duration: 1000,
if (!window) return

await tts
.setMetadata(input, OUTPUT_FORMAT.WEBM_24KHZ_16BIT_MONO_OPUS)
.catch((error: unknown) => {
console.error("Failed to set voice", error)
if (error instanceof Error) {
return callWindowExpose(window).toast.error(error.message, {
duration: 1000,
})
}
return callWindowExpose(window).toast.error("Failed to set voice", {
duration: 1000,
})
})
})
}),

detectCodeStringLanguage: t.procedure
Expand Down
7 changes: 6 additions & 1 deletion packages/shared/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ function createProxy<T extends RenderGlobalContext>(window: BrowserWindow, path:

try {
return await window.webContents.executeJavaScript(
`(async () => { try { return await globalThis.${PREFIX}?.${methodPath}?.(${args.map((arg) => JSON.stringify(arg)).join(",")}) } catch (err) { console.error('Failed to executeJavaScript: ${methodPath}', err) } })()`,
`globalThis.${PREFIX}?.${methodPath}?.(${args
.map((arg) => {
if (arg === undefined) return "undefined"
return JSON.stringify(arg)
})
.join(",")})`,
)
} catch (err) {
console.error(`Failed to executeJavaScript: ${methodPath}`, err)
Expand Down

0 comments on commit 51519e2

Please sign in to comment.