refactor(tracer-context): 引入ScopeError类并优化错误处理逻辑

- 新增ScopeError自定义错误类,用于区分范围相关错误
- 将error字段重命名为exception以更准确描述其用途
- 优化flush方法,仅在有必要时输出指令序列
- 简化错误处理逻辑,移除冗余的console.error调用
- 确保ScopeError能正确向上抛出而不被捕获
This commit is contained in:
2026-02-23 03:20:13 +08:00
parent 134d4e2b19
commit 9f08c34225

View File

@@ -1,5 +1,12 @@
import type { TracerCommand } from '../types'; import type { TracerCommand } from '../types';
class ScopeError extends Error {
constructor(message?: string) {
super(message);
this.name = 'ScopeError';
}
}
const SCOPE_STATE = { const SCOPE_STATE = {
INITIAL: 'initial', INITIAL: 'initial',
SCOPING: 'scoping', SCOPING: 'scoping',
@@ -15,8 +22,8 @@ type Error = {
type ScopeResult = { type ScopeResult = {
// 记录指令序列 // 记录指令序列
commands: TracerCommand[]; commands: TracerCommand[];
// 记录错误信息 // 记录异常信息
error: Error; exception: Error;
// 检查scope的执行状态 // 检查scope的执行状态
state: ScopeState; state: ScopeState;
}; };
@@ -24,7 +31,7 @@ type ScopeResult = {
const createTracerContext = () => { const createTracerContext = () => {
const scopeResult: ScopeResult = { const scopeResult: ScopeResult = {
commands: [], commands: [],
error: {}, exception: {},
state: SCOPE_STATE.INITIAL, state: SCOPE_STATE.INITIAL,
}; };
@@ -34,15 +41,12 @@ const createTracerContext = () => {
// TODO: 输出指令序列和错误信息 // TODO: 输出指令序列和错误信息
const flush = () => { const flush = () => {
const { commands, error } = scopeResult; const { commands, exception } = scopeResult;
// if (commands.length > 0 || !!error.stack) { if (commands.length > 0 || !!exception.stack) {
// console.log('输出指令序列'); console.log(JSON.stringify({ commands, exception }, null, 2));
// console.log(JSON.stringify({ commands, error }, null, 2)); }
// }
console.log('输出指令序列');
console.log(JSON.stringify({ commands, error }, null, 2));
scopeResult.commands = []; scopeResult.commands = [];
scopeResult.error = {}; scopeResult.exception = {};
}; };
const getTracerContext = () => { const getTracerContext = () => {
@@ -54,12 +58,10 @@ const createTracerContext = () => {
const scope = (routine: () => void | Promise<void>) => { const scope = (routine: () => void | Promise<void>) => {
if (scopeResult.state === SCOPE_STATE.SCOPED) { if (scopeResult.state === SCOPE_STATE.SCOPED) {
console.error('多次调用scope方法'); throw new ScopeError('ScopeError: Detect multiple scope.');
throw new Error('[TracerContext] Detect multiple scope.');
} }
if (scopeResult.state === SCOPE_STATE.SCOPING) { if (scopeResult.state === SCOPE_STATE.SCOPING) {
console.error('嵌套调用scope方法'); throw new ScopeError('ScopeError: Detect nested scope.');
throw new Error('[TracerContext] Detect nested scope.');
} }
scopeResult.state = SCOPE_STATE.SCOPING; scopeResult.state = SCOPE_STATE.SCOPING;
// 如果routine是同步函数那可能会抛出同步错误所以需要用try-catch包裹 // 如果routine是同步函数那可能会抛出同步错误所以需要用try-catch包裹
@@ -67,10 +69,11 @@ const createTracerContext = () => {
const result = routine(); const result = routine();
if (result instanceof Promise) { if (result instanceof Promise) {
result result
.catch((err) => { .catch((error) => {
console.log('异步错误'); if (error instanceof ScopeError) {
console.error(err); throw error;
scopeResult.error.stack = (err as Error).stack; }
scopeResult.exception.stack = (error as Error).stack;
}) })
.finally(() => { .finally(() => {
scopeResult.state = SCOPE_STATE.SCOPED; scopeResult.state = SCOPE_STATE.SCOPED;
@@ -80,10 +83,11 @@ const createTracerContext = () => {
scopeResult.state = SCOPE_STATE.SCOPED; scopeResult.state = SCOPE_STATE.SCOPED;
flush(); flush();
} }
} catch (err) { } catch (error) {
console.log('同步错误'); if (error instanceof ScopeError) {
console.error(err); throw error;
scopeResult.error.stack = (err as Error).stack; }
scopeResult.exception.stack = (error as Error).stack;
scopeResult.state = SCOPE_STATE.SCOPED; scopeResult.state = SCOPE_STATE.SCOPED;
flush(); flush();
} }