Skip to content

Commit

Permalink
feat(script and core): 添加全局错误捕获功能
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Mar 19, 2023
1 parent 23df64d commit 476483d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
38 changes: 27 additions & 11 deletions packages/core/src/interfaces/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ type ScriptEvent = {
render: (elements: { panel: ScriptPanelElement; header: HeaderElement }) => any;
/** 在页面离开时执行的事件 */
beforeunload: (...args: any[]) => undefined | boolean;
/** 发生错误时 */
scripterror: (err: string) => void;
};

export class BaseScript<E extends Record<string | symbol, (...args: any[]) => any>> extends CommonEventEmitter<E> {
export class BaseScript<E extends ScriptEvent = ScriptEvent> extends CommonEventEmitter<E> {
/** 在脚本加载时立即运行的钩子 */
onstart?: (...args: any[]) => any;
/** 在页面初始化完成时(元素可被访问)时运行的钩子 */
Expand All @@ -59,15 +61,14 @@ export class BaseScript<E extends Record<string | symbol, (...args: any[]) => an
onrender?: (elements: { panel: ScriptPanelElement; header: HeaderElement }) => any;
/** 在页面离开时执行的钩子 */
onbeforeunload?: (...args: any[]) => undefined | boolean;
/** 发生错误时 */
onerror?: (err: string) => void;
}

/**
* 脚本
*/
export class Script<
T extends ScriptConfigs = ScriptConfigs,
Events extends ScriptEvent = ScriptEvent
> extends BaseScript<Events> {
export class Script<T extends ScriptConfigs = ScriptConfigs> extends BaseScript<ScriptEvent> {
/** 未经处理的 configs 原对象 */
private _configs?: ScriptConfigsProvider<T>;
/** 存储已经处理过的 configs 对象,避免重复调用方法 */
Expand Down Expand Up @@ -134,12 +135,12 @@ export class Script<
this.excludes = excludes;
this._configs = configs;
this.hideInPanel = hideInPanel;
this.onstart = onstart;
this.onactive = onactive;
this.oncomplete = oncomplete;
this.onbeforeunload = onbeforeunload;
this.onrender = onrender;
this.onhistorychange = onhistorychange;
this.onstart = this.errorHandler(onstart);
this.onactive = this.errorHandler(onactive);
this.oncomplete = this.errorHandler(oncomplete);
this.onbeforeunload = this.errorHandler(onbeforeunload);
this.onrender = this.errorHandler(onrender);
this.onhistorychange = this.errorHandler(onhistorychange);
}

onConfigChange<K extends keyof T>(
Expand All @@ -156,4 +157,19 @@ export class Script<
offConfigChange(listener: number | EventListener) {
$store.removeChangeListener(listener);
}

private errorHandler(func?: Function) {
return (...args: any[]) => {
try {
return func?.apply(this, args);
} catch (err) {
console.error(err);
if (err instanceof Error) {
this.emit('scripterror', err.message);
} else {
this.emit('scripterror', String(err));
}
}
};
}
}
19 changes: 19 additions & 0 deletions packages/scripts/src/projects/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,25 @@ export const BackgroundProject = Project.create({
}, 60 * 1000);
}
}
}),
errorHandle: new Script({
name: '全局错误捕获',
url: [['', /.*/]],
onstart() {
const projects = definedProjects();
for (const project of projects) {
for (const key in project.scripts) {
if (Object.prototype.hasOwnProperty.call(project.scripts, key)) {
const script = project.scripts[key];
script.on('scripterror', (err) => {
const msg = `[${project.name} - ${script.name}] : ${err}`;
console.error(msg);
$console.error(msg);
});
}
}
}
}
})
}
});
Expand Down

0 comments on commit 476483d

Please sign in to comment.