refactor(tracer-context): 引入ScopeError类并优化错误处理逻辑
- 新增ScopeError自定义错误类,用于区分范围相关错误 - 将error字段重命名为exception以更准确描述其用途 - 优化flush方法,仅在有必要时输出指令序列 - 简化错误处理逻辑,移除冗余的console.error调用 - 确保ScopeError能正确向上抛出而不被捕获
This commit is contained in:
@@ -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<void>) => {
|
||||
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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user