From 9f08c342253c31e7d68cb532294cc6eccf0fbfec Mon Sep 17 00:00:00 2001 From: skycurtain Date: Mon, 23 Feb 2026 03:20:13 +0800 Subject: [PATCH] =?UTF-8?q?refactor(tracer-context):=20=E5=BC=95=E5=85=A5S?= =?UTF-8?q?copeError=E7=B1=BB=E5=B9=B6=E4=BC=98=E5=8C=96=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增ScopeError自定义错误类,用于区分范围相关错误 - 将error字段重命名为exception以更准确描述其用途 - 优化flush方法,仅在有必要时输出指令序列 - 简化错误处理逻辑,移除冗余的console.error调用 - 确保ScopeError能正确向上抛出而不被捕获 --- tracers.ts/src/context/tracer-context.ts | 50 +++++++++++++----------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/tracers.ts/src/context/tracer-context.ts b/tracers.ts/src/context/tracer-context.ts index 1f84e44..c4947a1 100644 --- a/tracers.ts/src/context/tracer-context.ts +++ b/tracers.ts/src/context/tracer-context.ts @@ -1,5 +1,12 @@ import type { TracerCommand } from '../types'; +class ScopeError extends Error { + constructor(message?: string) { + super(message); + this.name = 'ScopeError'; + } +} + const SCOPE_STATE = { INITIAL: 'initial', SCOPING: 'scoping', @@ -15,8 +22,8 @@ type Error = { type ScopeResult = { // 记录指令序列 commands: TracerCommand[]; - // 记录错误信息 - error: Error; + // 记录异常信息 + exception: Error; // 检查scope的执行状态 state: ScopeState; }; @@ -24,7 +31,7 @@ type ScopeResult = { const createTracerContext = () => { const scopeResult: ScopeResult = { commands: [], - error: {}, + exception: {}, state: SCOPE_STATE.INITIAL, }; @@ -34,15 +41,12 @@ const createTracerContext = () => { // TODO: 输出指令序列和错误信息 const flush = () => { - const { commands, error } = scopeResult; - // if (commands.length > 0 || !!error.stack) { - // console.log('输出指令序列'); - // console.log(JSON.stringify({ commands, error }, null, 2)); - // } - console.log('输出指令序列'); - console.log(JSON.stringify({ commands, error }, null, 2)); + const { commands, exception } = scopeResult; + if (commands.length > 0 || !!exception.stack) { + console.log(JSON.stringify({ commands, exception }, null, 2)); + } scopeResult.commands = []; - scopeResult.error = {}; + scopeResult.exception = {}; }; const getTracerContext = () => { @@ -54,12 +58,10 @@ const createTracerContext = () => { const scope = (routine: () => void | Promise) => { if (scopeResult.state === SCOPE_STATE.SCOPED) { - console.error('多次调用scope方法'); - throw new Error('[TracerContext] Detect multiple scope.'); + throw new ScopeError('ScopeError: Detect multiple scope.'); } if (scopeResult.state === SCOPE_STATE.SCOPING) { - console.error('嵌套调用scope方法'); - throw new Error('[TracerContext] Detect nested scope.'); + throw new ScopeError('ScopeError: Detect nested scope.'); } scopeResult.state = SCOPE_STATE.SCOPING; // 如果routine是同步函数,那可能会抛出同步错误,所以需要用try-catch包裹 @@ -67,10 +69,11 @@ const createTracerContext = () => { const result = routine(); if (result instanceof Promise) { result - .catch((err) => { - console.log('异步错误'); - console.error(err); - scopeResult.error.stack = (err as Error).stack; + .catch((error) => { + if (error instanceof ScopeError) { + throw error; + } + scopeResult.exception.stack = (error as Error).stack; }) .finally(() => { scopeResult.state = SCOPE_STATE.SCOPED; @@ -80,10 +83,11 @@ const createTracerContext = () => { scopeResult.state = SCOPE_STATE.SCOPED; flush(); } - } catch (err) { - console.log('同步错误'); - console.error(err); - scopeResult.error.stack = (err as Error).stack; + } catch (error) { + if (error instanceof ScopeError) { + throw error; + } + scopeResult.exception.stack = (error as Error).stack; scopeResult.state = SCOPE_STATE.SCOPED; flush(); }