From b5d115b7131a99bec31c2316836c9f1c227e92eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E4=BA=9A=E6=98=95?= <1549469775@qq.com> Date: Wed, 10 Sep 2025 17:26:33 +0800 Subject: [PATCH] =?UTF-8?q?fix(middlewares):=20=E4=BF=AE=E6=AD=A3=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E5=A4=84=E7=90=86=E4=B8=AD=E5=93=8D=E5=BA=94=E5=86=85?= =?UTF-8?q?=E5=AE=B9=E7=B1=BB=E5=9E=8B=E5=8F=8A=E5=A2=9E=E5=BC=BA=E5=BC=80?= =?UTF-8?q?=E5=8F=91=E7=8E=AF=E5=A2=83=E9=94=99=E8=AF=AF=E5=A0=86=E6=A0=88?= =?UTF-8?q?=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将非HTML错误响应的内容类型从json改为text - 调整错误响应体格式,统一为纯文本格式及状态信息 - 在开发环境中错误捕获时输出完整的错误堆栈到控制台 - 优化错误处理逻辑,提升调试体验和错误信息可读性 --- src/middlewares/errorHandler/index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/middlewares/errorHandler/index.js b/src/middlewares/errorHandler/index.js index 9acb84f..816dce4 100644 --- a/src/middlewares/errorHandler/index.js +++ b/src/middlewares/errorHandler/index.js @@ -12,7 +12,7 @@ async function formatError(ctx, status, message, stack) { ctx.type = "html" await ctx.render("error/index", { status, message, stack, isDev }) } else { - ctx.type = "json" + ctx.type = "text" ctx.body = isDev && stack ? `${status} - ${message}\n${stack}` : `${status} - ${message}` } ctx.status = status @@ -34,6 +34,9 @@ export default function errorHandler() { } catch (err) { logger.error(err) const isDev = process.env.NODE_ENV === "development" + if (isDev && err.stack) { + console.error(err.stack) + } await formatError(ctx, err.statusCode || 500, err.message || err || "Internal server error", isDev ? err.stack : undefined) } }