Skip to content

Commit

Permalink
fix: handle error for addons
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Oct 24, 2023
1 parent 7e6644e commit 626efb1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 23 deletions.
32 changes: 19 additions & 13 deletions api/modules/addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ interface AddonContext {

const loadedAddonContexts: AddonContext[] = []

function loadAddonEntry(addon: AddonInfo): APIAddon {
if (addon.name === 'custom.js') {
try {
const userDataPath = app.isPackaged()
? path.join(app.getPath('userData'), 'User')
: path.join(app.getPath(), 'userdata')
return require(path.join(userDataPath, addon.name))
} catch {
return () => {/* noop */}
}
}
return require(addon.entry)
}

function loadAddon(addon: AddonInfo, api: CompatableAPI) {
if (loadedAddonContexts.some(item => item.addon.name === addon.name)) return
// Reserved names
Expand All @@ -84,20 +98,12 @@ function loadAddon(addon: AddonInfo, api: CompatableAPI) {
// Reactivity scope
const scope = effectScope()
scope.run(() => {
let processor: APIAddon
if (addon.name === 'custom.js') {
try {
const userDataPath = app.isPackaged()
? path.join(app.getPath('userData'), 'User')
: path.join(app.getPath(), 'userdata')
processor = require(path.join(userDataPath, addon.name))
} catch {
processor = () => {/* noop */}
}
} else {
processor = require(addon.entry)
try {
const processor = loadAddonEntry(addon)
processor(clonedAPI)
} catch (err) {
app.triggerError(err)
}
processor(clonedAPI)
})
addCommasModule(undefined)
loadedAddonContexts.push({ addon, scope })
Expand Down
9 changes: 9 additions & 0 deletions api/modules/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ function getManifest(): Record<string, any> {
return require(path.join(getPath(), 'package.json'))
}

function triggerError(err: Error) {
if (isMainProcess()) {
process.emit('uncaughtException', err)
} else {
console.error(err)
}
}

function onCleanup(this: APIContext, callback: () => void) {
events.once(`unload:${this.__name__}`, callback)
}
Expand Down Expand Up @@ -78,6 +86,7 @@ export {
getPath,
getVersion,
getManifest,
triggerError,
onCleanup,
effect,
onInvalidate,
Expand Down
8 changes: 4 additions & 4 deletions api/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type * as Main from './main'
import type * as Renderer from './renderer'
import type * as main from './main'
import type * as renderer from './renderer'

type MixValues<T, U> = {
[K in keyof T]: T[K] & U
}

export type MainAPI = typeof Main
export type RendererAPI = typeof Renderer
export type MainAPI = typeof main
export type RendererAPI = typeof renderer
export type API = MainAPI & RendererAPI
export type CompatableAPI = MainAPI | RendererAPI

Expand Down
2 changes: 1 addition & 1 deletion build/pack.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const options = {
? pkg.name : pkg.productName,
out: 'release/',
overwrite: true,
asar: {
asar: process.env.DEBUG ? false : {
// FIXME: node-pty does not support app.asar.unpacked currently
unpack: '**/{*.node,node_modules/node-pty/**/*}',
},
Expand Down
15 changes: 10 additions & 5 deletions src/main/lib/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ import * as fileIcon from 'file-icon'
import { readFile, watchFile, writeFile } from '../utils/file'
import { execa, until } from '../utils/helper'
import { notify } from '../utils/notification'
import { broadcast } from './frame'
import { broadcast, hasWindow } from './frame'

let currentBouncingId = -1

function reportError(error: Error) {
console.error(error)
if (hasWindow()) {
broadcast('uncaught-error', String(error))
}
}

function handleMessages() {
process.on('uncaughtException', error => {
console.error(error)
broadcast('uncaught-error', String(error))
reportError(error)
})
process.on('unhandledRejection', (error: Error) => {
console.error(error)
broadcast('uncaught-error', String(error))
reportError(error)
})
app.on('before-quit', () => {
broadcast('before-quit')
Expand Down

0 comments on commit 626efb1

Please sign in to comment.