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
This commit is contained in:
Whitewater 2024-12-02 15:21:25 +08:00 committed by GitHub
parent 22e2c0d649
commit 51519e2d7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 14 deletions

View File

@ -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
}
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,
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

View File

@ -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)