Compare commits

..

3 Commits

Author SHA1 Message Date
9f08c34225 refactor(tracer-context): 引入ScopeError类并优化错误处理逻辑
- 新增ScopeError自定义错误类,用于区分范围相关错误
- 将error字段重命名为exception以更准确描述其用途
- 优化flush方法,仅在有必要时输出指令序列
- 简化错误处理逻辑,移除冗余的console.error调用
- 确保ScopeError能正确向上抛出而不被捕获
2026-02-23 03:20:13 +08:00
134d4e2b19 refactor(tracer-context): 改进 scope 状态管理以支持嵌套检测
- 引入 SCOPE_STATE 枚举明确区分初始、执行中和已完成状态
- 将 scoping 布尔标志替换为 state 枚举,支持检测嵌套调用
- 分离同步和异步错误处理流程,确保状态正确更新
- 修复 flush 方法中 commands 和 error 的清理逻辑
2026-02-23 02:05:58 +08:00
8894458f4c feat(tracer-context): 添加scope方法以支持追踪异步错误
新增scope方法用于包裹异步或同步代码块,自动捕获错误并记录到上下文中。同时将dump方法重命名为flush以更准确描述其行为(清空并输出指令序列)。重构内部数据结构,将commands和error统一管理在scopeResult对象中。
2026-02-22 22:37:06 +08:00

View File

@@ -1,35 +1,102 @@
import type { TracerCommand } from '../types'; import type { TracerCommand } from '../types';
const createTracerContext = () => { class ScopeError extends Error {
const commands: TracerCommand[] = []; constructor(message?: string) {
super(message);
this.name = 'ScopeError';
}
}
// if (typeof process !== 'undefined' && typeof process.on === 'function') { const SCOPE_STATE = {
// process.on('exit', () => { INITIAL: 'initial',
// if (commands.length > 0) { SCOPING: 'scoping',
// console.log(commands); SCOPED: 'scoped',
// } } as const;
// });
// } type ScopeState = (typeof SCOPE_STATE)[keyof typeof SCOPE_STATE];
type Error = {
stack?: string;
};
type ScopeResult = {
// 记录指令序列
commands: TracerCommand[];
// 记录异常信息
exception: Error;
// 检查scope的执行状态
state: ScopeState;
};
const createTracerContext = () => {
const scopeResult: ScopeResult = {
commands: [],
exception: {},
state: SCOPE_STATE.INITIAL,
};
const command = (command: TracerCommand) => {
scopeResult.commands.push(command);
};
// TODO: 输出指令序列和错误信息
const flush = () => {
const { commands, exception } = scopeResult;
if (commands.length > 0 || !!exception.stack) {
console.log(JSON.stringify({ commands, exception }, null, 2));
}
scopeResult.commands = [];
scopeResult.exception = {};
};
const getTracerContext = () => { const getTracerContext = () => {
const command = (command: TracerCommand) => {
commands.push(command);
};
// TODO: 输出指令序列
const dump = () => {
return commands;
};
return { return {
command, command,
dump, flush,
}; };
}; };
const scope = (routine: () => void | Promise<void>) => {
if (scopeResult.state === SCOPE_STATE.SCOPED) {
throw new ScopeError('ScopeError: Detect multiple scope.');
}
if (scopeResult.state === SCOPE_STATE.SCOPING) {
throw new ScopeError('ScopeError: Detect nested scope.');
}
scopeResult.state = SCOPE_STATE.SCOPING;
// 如果routine是同步函数那可能会抛出同步错误所以需要用try-catch包裹
try {
const result = routine();
if (result instanceof Promise) {
result
.catch((error) => {
if (error instanceof ScopeError) {
throw error;
}
scopeResult.exception.stack = (error as Error).stack;
})
.finally(() => {
scopeResult.state = SCOPE_STATE.SCOPED;
flush();
});
} else {
scopeResult.state = SCOPE_STATE.SCOPED;
flush();
}
} catch (error) {
if (error instanceof ScopeError) {
throw error;
}
scopeResult.exception.stack = (error as Error).stack;
scopeResult.state = SCOPE_STATE.SCOPED;
flush();
}
};
return { return {
getTracerContext, getTracerContext,
scope,
}; };
}; };
export const { getTracerContext } = createTracerContext(); export const { getTracerContext, scope } = createTracerContext();