阶段4:推进启动稳定性与组合根注入

This commit is contained in:
Christ committed 2026-08-18 19:51:38 +08:00
1 parent 9da450d940
commit d2d8e02a39
36 files changed
+830 -168

No files matched your search

+11 -3
View File
@@ -1,5 +1,5 @@
import { afterEach, describe, expect, test, vi } from 'vitest';
import { del, get, post, postForm, put } from './client';
import { ApiError, del, get, post, postForm, put } from './client';
afterEach(() => vi.unstubAllGlobals() /* 清理测试安装的全局网络替身,避免跨用例泄漏。 */);
@@ -48,11 +48,19 @@ describe('request helpers', () => {
} /* 测试回调验证:支持 PUT 和 DELETE 请求及查询参数。 */);
test('surfaces unified backend errors', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ code: 'bad_request', message: 'bad request' }), {
// payload 是服务端返回的统一错误 envelope,包含调用方恢复流程需要的追踪和细节字段。
const payload = { code: 'bad_request', message: 'bad request', request_id: 'req-1', details: { field: 'title' } };
vi.stubGlobal('fetch', vi.fn().mockImplementation(/* responseFactory 为每次断言提供未被消费的独立错误响应。 */ () => Promise.resolve(new Response(JSON.stringify(payload), {
status: 400,
headers: { 'content-type': 'application/json' },
})));
}))));
await expect(get('/bad')).rejects.toThrow('bad request');
try {
await get('/bad');
} catch (error /* error 是普通 JSON 请求必须保留结构化 envelope 的异常。 */) {
expect(error).toBeInstanceOf(ApiError);
expect(error).toMatchObject({ status: 400, code: 'bad_request', requestId: 'req-1', details: { field: 'title' }, payload });
}
} /* 测试回调验证:surfaces unified backend errors。 */);
test('使用非 JSON 错误文本作为失败消息', async () => {
+38 -10
View File
@@ -1,3 +1,5 @@
import type { ApiErrorResponse } from '../api-contract';
type RequestMethod = 'GET' | 'POST' | 'PUT' | 'DELETE';
type QueryParams = Record<string, string | number | boolean | undefined | null>;
@@ -25,6 +27,31 @@ const uploadRequestTimeoutMs = 10 * 60_000; /* uploadRequestTimeoutMs 表示uplo
let authLogoutPending = false; /* authLogoutPending 表示authLogoutPending。 */
// ApiError 保留服务端统一错误 envelope,调用方可按稳定错误码决定重试、冲突提示或人工核对流程。
export class ApiError extends Error {
// status 是产生错误的 HTTP 状态码,供调用方区分授权、冲突和服务端失败。
readonly status: number;
// code 是服务端稳定机器错误码;非结构化响应退化为 http_<status>。
readonly code: string;
// requestId 是服务端请求追踪标识,用户反馈问题时可安全提供给运维。
readonly requestId?: string;
// details 是服务端提供的结构化恢复或审计信息,不包含客户端主动拼接的兼容字段。
readonly details?: Record<string, unknown>;
// payload 是保留给既有上传流程和诊断代码的原始响应载荷。
readonly payload: unknown;
// constructor 根据 HTTP 响应和统一错误载荷构造可被所有请求路径复用的异常实例。
constructor(status: number, payload: unknown) {
super(errorMessageFromPayload(payload, status));
this.name = 'ApiError';
this.status = status;
this.code = isApiErrorResponse(payload) ? payload.code : `http_${status}`;
this.requestId = isApiErrorResponse(payload) ? payload.request_id : undefined;
this.details = isApiErrorResponse(payload) ? payload.details : undefined;
this.payload = payload;
}
}
const notifyAuthExpired = () => {
if (authLogoutPending || typeof window === 'undefined') return;
authLogoutPending = true;
@@ -76,9 +103,8 @@ const request = async <T>(method: RequestMethod, url: string, options: RequestOp
if (!res.ok) {
if (res.status === 401 && !options.skipAuthLogout) notifyAuthExpired();
// payload 是后端统一错误 DTO 或非 JSON 错误文本。
const payload = isJson ? await res.json().catch(() => undefined /* 错误响应 JSON 无法解析时交由状态码兜底。 */) : await res.text().catch(() => undefined /* 错误响应文本无法读取时交由状态码兜底。 */);
const message = errorMessageFromPayload(payload, res.status); // message 是统一错误 DTO 提取出的用户可见说明。
throw new Error(message);
const payload = await readResponsePayload(res, isJson);
throw new ApiError(res.status, payload);
}
if (!isJson) {
@@ -115,14 +141,11 @@ export const postForm = async <T>(url: string, body: FormData, options: RequestC
const contentType = res.headers.get('content-type') || ''; /* contentType 表示contentType。 */
const isJson = contentType.includes('application/json'); /* isJson 表示isJson。 */
const payload = isJson ? await res.json().catch(() => undefined /* 上传错误 JSON 无法解析时交由状态码兜底。 */) : await res.text().catch(() => undefined /* 上传错误文本无法读取时交由状态码兜底。 */); /* payload 是上传接口返回的错误载荷,仅用于构造异常。 */
const payload = await readResponsePayload(res, isJson); /* payload 是上传接口返回的成功或错误载荷。 */
if (!res.ok) {
if (res.status === 401) notifyAuthExpired();
const message = errorMessageFromPayload(payload, res.status); // message 是上传失败时统一错误 DTO 提取出的说明。
const err = new Error(message) as Error & { /* payload 表示payload。 */ payload?: unknown }; /* err 表示当前操作返回的错误。 */
err.payload = payload;
throw err;
throw new ApiError(res.status, payload);
}
return payload as T;
@@ -143,6 +166,13 @@ const controlledSignal = (external: AbortSignal | undefined, timeoutMs: number)
};
}; /* controlledSignal 表示controlledSignal。 */
// readResponsePayload 按响应类型读取一次响应体;解析失败返回 undefined,由 ApiError 生成稳定回退信息。
const readResponsePayload = async (response: Response, isJson: boolean): Promise<unknown> => (
isJson
? response.json().catch(() => undefined /* 错误响应 JSON 无法解析时交由状态码兜底。 */)
: response.text().catch(() => undefined /* 错误响应文本无法读取时交由状态码兜底。 */)
);
/** 判断响应体是否符合统一 HTTP 错误 DTO。 */
const isApiErrorResponse = (payload: unknown): payload is ApiErrorResponse => {
if (typeof payload !== 'object' || payload === null) return false;
@@ -157,5 +187,3 @@ const errorMessageFromPayload = (payload: unknown, status: number): string => {
if (isApiErrorResponse(payload)) return payload.message;
return `请求失败: ${status}`;
};
import type { ApiErrorResponse } from '../api-contract';