Skip to content

Commit 9c03bab

Browse files
author
Taois
committed
feat: 完善调试日志确保错误能被捕获
1 parent ac4598a commit 9c03bab

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

controllers/api.js

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ export default (fastify, options, done) => {
116116

117117
// 检查模块文件是否存在
118118
if (!existsSync(modulePath)) {
119-
reply.status(404).send({error: `Module ${moduleName} not found`});
119+
const error_msg=`Module ${moduleName} not found`;
120+
console.error(error_msg);
121+
fastify.log.error(error_msg);
122+
reply.status(404).send({error: error_msg});
120123
return;
121124
}
122125

@@ -374,8 +377,10 @@ export default (fastify, options, done) => {
374377

375378
} catch (error) {
376379
// 错误处理和日志记录
377-
fastify.log.error(`Error api module ${moduleName}:${error.message}`);
378-
reply.status(500).send({error: `Failed to process module ${moduleName}: ${error.message}`});
380+
const error_msg=`Failed to process module ${moduleName}: ${error.message}`;
381+
console.error(error_msg);
382+
fastify.log.error(error_msg);
383+
reply.status(500).send({error: error_msg});
379384
}
380385
}
381386
});
@@ -396,7 +401,10 @@ export default (fastify, options, done) => {
396401

397402
// 检查模块文件是否存在
398403
if (!existsSync(modulePath)) {
399-
reply.status(404).send({error: `Module ${moduleName} not found`});
404+
const error_msg=`Module ${moduleName} not found`;
405+
console.error(error_msg);
406+
fastify.log.error(error_msg);
407+
reply.status(404).send({error: error_msg});
400408
return;
401409
}
402410

@@ -479,7 +487,9 @@ export default (fastify, options, done) => {
479487
}
480488
content = Buffer.from(content, 'base64');
481489
} catch (e) {
482-
fastify.log.error(`Local Proxy toBytes error: ${e}`);
490+
const error_msg = `Local Proxy toBytes error: ${e}`;
491+
fastify.log.error(error_msg);
492+
console.error(error_msg);
483493
}
484494
}
485495
// 流代理 - 重定向到媒体代理服务
@@ -524,9 +534,10 @@ export default (fastify, options, done) => {
524534
}
525535

526536
} catch (error) {
527-
// 错误处理和日志记录
528-
fastify.log.error(`Error proxy module ${moduleName}:${error.message}`);
529-
reply.status(500).send({error: `Failed to proxy module ${moduleName}: ${error.message}`});
537+
const error_msg = `Error proxy module ${moduleName}:${error.message}`;
538+
fastify.log.error(error_msg);
539+
console.error(error_msg);
540+
reply.status(500).send({error: error_msg});
530541
}
531542
});
532543

@@ -547,7 +558,10 @@ export default (fastify, options, done) => {
547558

548559
// 检查解析器文件是否存在
549560
if (!existsSync(jxPath)) {
550-
return reply.status(404).send({error: `解析 ${jxName} not found`});
561+
const error_msg = `解析 ${jxName} not found`;
562+
fastify.log.error(error_msg);
563+
console.error(error_msg);
564+
return reply.status(404).send({error: error_msg});
551565
}
552566

553567
const moduleExt = query.extend || '';
@@ -659,13 +673,18 @@ export default (fastify, options, done) => {
659673
return reply.code(statusCode).type(`${mediaType}; charset=utf-8`).send(backRespSend);
660674
} else {
661675
// 其他类型的响应,返回失败
662-
return reply.status(404).send({error: `${jxName}解析失败`});
676+
const error_msg = `${jxName}解析失败`;
677+
fastify.log.error(error_msg);
678+
console.error(error_msg);
679+
return reply.status(404).send({error: error_msg});
663680
}
664681

665682
} catch (error) {
666683
// 错误处理和日志记录
667-
fastify.log.error(`Error proxy jx ${jxName}:${error.message}`);
668-
reply.status(500).send({error: `Failed to proxy jx ${jxName}: ${error.message}`});
684+
const error_msg = `Failed to proxy jx ${jxName}: ${error.message}`;
685+
fastify.log.error(error_msg);
686+
console.error(error_msg);
687+
reply.status(500).send({error: error_msg});
669688
}
670689
});
671690

0 commit comments

Comments
 (0)