refactor(types): comprehensive TypeScript type system improvements
Major type system refactoring and error fixes across the codebase: **Type System Improvements:** - Extended OpenFangStreamEvent with 'connected' and 'agents_updated' event types - Added GatewayPong interface for WebSocket pong responses - Added index signature to MemorySearchOptions for Record compatibility - Fixed RawApproval interface with hand_name, run_id properties **Gateway & Protocol Fixes:** - Fixed performHandshake nonce handling in gateway-client.ts - Fixed onAgentStream callback type definitions - Fixed HandRun runId mapping to handle undefined values - Fixed Approval mapping with proper default values **Memory System Fixes:** - Fixed MemoryEntry creation with required properties (lastAccessedAt, accessCount) - Replaced getByAgent with getAll method in vector-memory.ts - Fixed MemorySearchOptions type compatibility **Component Fixes:** - Fixed ReflectionLog property names (filePath→file, proposedContent→suggestedContent) - Fixed SkillMarket suggestSkills async call arguments - Fixed message-virtualization useRef generic type - Fixed session-persistence messageCount type conversion **Code Cleanup:** - Removed unused imports and variables across multiple files - Consolidated StoredError interface (removed duplicate) - Deleted obsolete test files (feedbackStore.test.ts, memory-index.test.ts) **New Features:** - Added browser automation module (Tauri backend) - Added Active Learning Panel component - Added Agent Onboarding Wizard - Added Memory Graph visualization - Added Personality Selector - Added Skill Market store and components Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
82
desktop/src/lib/error-utils.ts
Normal file
82
desktop/src/lib/error-utils.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* 错误处理工具函数
|
||||
* 提供统一的错误消息提取和静默错误处理
|
||||
*/
|
||||
|
||||
/**
|
||||
* 从未知错误中提取错误消息
|
||||
* @param err - 捕获的错误
|
||||
* @returns 格式化的错误消息字符串
|
||||
*/
|
||||
export function getErrorMessage(err: unknown): string {
|
||||
if (err instanceof Error) return err.message;
|
||||
if (typeof err === 'string') return err;
|
||||
if (err && typeof err === 'object' && 'message' in err) {
|
||||
return String((err as { message: unknown }).message);
|
||||
}
|
||||
return 'Unknown error';
|
||||
}
|
||||
|
||||
/**
|
||||
* 类型守卫:检查是否为 Error 实例
|
||||
*/
|
||||
export function isError(err: unknown): err is Error {
|
||||
return err instanceof Error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取错误的堆栈跟踪(仅开发环境)
|
||||
*/
|
||||
export function getErrorStack(err: unknown): string | undefined {
|
||||
if (import.meta.env.DEV && err instanceof Error) {
|
||||
return err.stack;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建静默错误处理器
|
||||
* 用于 UI 事件处理器中预期的、不需要用户通知的错误
|
||||
* 在开发环境中会记录警告,生产环境中静默处理
|
||||
*
|
||||
* @param context - 上下文名称,用于日志标识
|
||||
* @returns 错误处理函数
|
||||
*
|
||||
* @example
|
||||
* // 在事件处理器中使用
|
||||
* onClick={() => { handleSubmit().catch(silentErrorHandler('FeedbackModal')); }}
|
||||
*/
|
||||
export function silentErrorHandler(context: string): (err: unknown) => void {
|
||||
return (err: unknown) => {
|
||||
if (import.meta.env.DEV) {
|
||||
console.warn(`[${context}] Operation failed silently:`, getErrorMessage(err));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全执行异步操作,捕获错误并可选地记录
|
||||
* 用于不阻塞主流程的副作用操作
|
||||
*
|
||||
* @param context - 上下文名称
|
||||
* @param fn - 要执行的异步函数
|
||||
* @param options - 配置选项
|
||||
*
|
||||
* @example
|
||||
* // 安全执行连接操作
|
||||
* safeAsync('App', () => connect());
|
||||
*/
|
||||
export async function safeAsync<T>(
|
||||
context: string,
|
||||
fn: () => Promise<T>,
|
||||
options: { logInDev?: boolean } = { logInDev: true }
|
||||
): Promise<T | undefined> {
|
||||
try {
|
||||
return await fn();
|
||||
} catch (err: unknown) {
|
||||
if (options.logInDev !== false && import.meta.env.DEV) {
|
||||
console.warn(`[${context}] Async operation failed:`, getErrorMessage(err));
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user