feat: 补充注释

This commit is contained in:
2026-04-26 01:13:38 +08:00
parent e26e2f5491
commit 4c58f409e9
23 changed files with 206 additions and 95 deletions
+9 -26
View File
@@ -1,5 +1,8 @@
// server/utils/createApiResponse.ts - 统一封装后端接口成功/失败响应,并把内部异常转换为安全前端文案。
import type { ICommonResponse } from "#shared/types";
const INTERNAL_SERVER_ERROR_MESSAGE = "服务器内部错误";
/**
* 构造统一成功响应。
*
@@ -39,37 +42,17 @@ export const createErrorResponse = (
};
/**
* 将上游请求错误转换为统一响应。
* 将上游或内部异常转换为统一响应。
*
* @param error ofetch 抛出的错误对象
* @param fallbackMessage 当上游没有提供可用错误信息时的兜底提示
* @returns 符合 ICommonResponse 格式的错误响应
* 调用方必须先在服务端日志中记录 error。这里永远不把上游 message、响应体、
* statusMessage 或 data 返回给前端,避免暴露内部服务、供应商、token 使用细节。
*/
export const createUpstreamErrorResponse = (
error: unknown,
fallbackMessage: string = "请求失败"
_fallbackMessage: string = INTERNAL_SERVER_ERROR_MESSAGE
): ICommonResponse => {
const fetchError = error as {
data?: unknown;
message?: string;
response?: {
_data?: unknown;
status?: number;
};
statusCode?: number;
};
const errorData = fetchError.data ?? fetchError.response?._data ?? null;
const errorMessage =
typeof errorData === "string"
? errorData
: fetchError.message || fallbackMessage;
return createErrorResponse(
fetchError.statusCode ?? fetchError.response?.status ?? 500,
errorMessage,
errorData
);
void error;
return createErrorResponse(500, INTERNAL_SERVER_ERROR_MESSAGE);
};
/** 判断上游或本地鉴权错误是否为 401,用于统一清理失效登录态 */