From 457e69f46bf43e03cf7f04a14e3c1e5951eb5c02 Mon Sep 17 00:00:00 2001 From: imeepos Date: Wed, 3 Sep 2025 18:44:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E4=B8=AD=E7=9A=84=E5=BE=AA=E7=8E=AF=E5=BC=95?= =?UTF-8?q?=E7=94=A8=E5=92=8Cundefined=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复executeTemplate中result.length访问undefined的问题 - 添加安全的类型检查和结果输出 - 修复JSON.stringify循环引用错误 - 改进异常处理的安全性 - 优化模板信息输出,避免复杂对象序列化问题 - 增强错误处理的健壮性 --- src/app.controller.ts | 49 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/src/app.controller.ts b/src/app.controller.ts index bf68058..76e1be2 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -19,7 +19,14 @@ export class AppController { const templates = this.tempalte.getAllTemplates(); console.log(`✅ [获取模板列表] 成功获取 ${templates.length} 个模板`); - console.log(`📊 模板列表: ${templates.map(t => t.code || t.name).join(', ')}`); + + // 安全地输出模板列表 + try { + const templateNames = templates.map(t => t?.code || t?.name || '[未命名模板]').filter(Boolean); + console.log(`📊 模板列表: ${templateNames.join(', ')}`); + } catch (error) { + console.log(`📊 模板列表: [无法获取模板名称]`); + } return { status: true, @@ -37,7 +44,19 @@ export class AppController { if (template) { console.log(`✅ [获取模板] 模板找到: ${template.name || templateCode}`); - console.log(`📋 模板信息: ${JSON.stringify(template, null, 2)}`); + + // 安全地输出模板信息,避免循环引用 + try { + const templateInfo = { + code: template.code, + name: template.name, + description: template.description, + // 只输出基本信息,避免复杂对象 + }; + console.log(`📋 模板信息: ${JSON.stringify(templateInfo, null, 2)}`); + } catch (jsonError) { + console.log(`📋 模板信息: [无法序列化的复杂对象]`); + } return { status: true, @@ -90,7 +109,17 @@ export class AppController { if (result) { console.log(`🎉 [模板执行] 执行成功`); - console.log(`📊 执行结果长度: ${result.length} 字符`); + console.log(`📊 执行结果类型: ${typeof result}`); + + // 安全地获取结果长度 + if (typeof result === 'string') { + console.log(`📊 执行结果长度: ${result.length} 字符`); + } else if (result && typeof result === 'object') { + console.log(`📊 执行结果: [对象类型]`); + } else { + console.log(`📊 执行结果: ${result}`); + } + console.log(`⏱️ 执行耗时: ${executionTime}ms`); console.log(`✨ 完成时间: ${new Date().toISOString()}`); @@ -101,7 +130,8 @@ export class AppController { }; } - console.log(`⚠️ [模板执行] 执行完成但结果为空`); + console.log(`⚠️ [模板执行] 执行完成但结果为空或undefined`); + console.log(`📊 结果值: ${result}`); console.log(`⏱️ 执行耗时: ${executionTime}ms`); return { @@ -112,15 +142,20 @@ export class AppController { } catch (e) { const executionTime = Date.now() - startTime; console.log(`💥 [模板执行] 执行异常`); - console.log(`❌ 错误信息: ${e.message}`); - console.log(`📍 错误堆栈: ${e.stack}`); + console.log(`❌ 错误信息: ${e?.message || '未知错误'}`); + + // 安全地输出错误堆栈 + if (e?.stack) { + console.log(`📍 错误堆栈: ${e.stack}`); + } + console.log(`⏱️ 执行耗时: ${executionTime}ms`); console.log(`🔚 异常时间: ${new Date().toISOString()}`); return { status: false, data: null, - msg: e.message + msg: e?.message || '模板执行异常' }; } }