refactor(*): 稳定期治理 - 收敛核心架构、错误语义并修复跨项目数据竞态
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
import axios from "axios";
|
||||
|
||||
import { apiClient, ApiError, request } from "@/services/api/request";
|
||||
import { http, ApiError } from "@/services/api/request";
|
||||
import type { ClientDiagnosticEvent } from "./client-diagnostics";
|
||||
|
||||
export type DiagnosticExportInput = {
|
||||
@@ -35,21 +33,21 @@ export type DiagnosticDownload = {
|
||||
};
|
||||
|
||||
export function previewDiagnosticBundle(input: DiagnosticExportInput) {
|
||||
return request<DiagnosticPreview>(apiClient.post("/diagnostics/preview", input));
|
||||
return http.post<DiagnosticPreview>("/diagnostics/preview", input);
|
||||
}
|
||||
|
||||
export async function exportDiagnosticBundle(input: DiagnosticExportInput): Promise<DiagnosticDownload> {
|
||||
try {
|
||||
const response = await apiClient.post<Blob>("/diagnostics/export", input, { responseType: "blob" });
|
||||
const response = await http.raw<Blob>({ method: "post", url: "/diagnostics/export", data: input, responseType: "blob" });
|
||||
return {
|
||||
blob: response.data,
|
||||
bundleId: readHeader(response.headers, "x-diagnostic-bundle-id") || "",
|
||||
fileName: parseFileName(readHeader(response.headers, "content-disposition")) || "yingce-diagnostics.zip",
|
||||
};
|
||||
} catch (error) {
|
||||
if (axios.isAxiosError(error) && error.response) {
|
||||
const status = error.response.status;
|
||||
const raw = await readErrorBody(error.response.data);
|
||||
if (error instanceof ApiError) {
|
||||
const status = error.status;
|
||||
const raw = await readErrorBody(responseDataFromCause(error));
|
||||
try {
|
||||
const payload = raw ? (JSON.parse(raw) as { code?: number; msg?: string }) : null;
|
||||
if (payload) {
|
||||
@@ -70,6 +68,14 @@ export async function exportDiagnosticBundle(input: DiagnosticExportInput): Prom
|
||||
}
|
||||
}
|
||||
|
||||
function responseDataFromCause(error: ApiError): unknown {
|
||||
const cause = error.cause;
|
||||
if (cause && typeof cause === "object" && "response" in cause) {
|
||||
return (cause as { response?: { data?: unknown } }).response?.data;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
async function readErrorBody(data: unknown) {
|
||||
if (data instanceof Blob) return data.text();
|
||||
return typeof data === "string" ? data : "";
|
||||
|
||||
Reference in New Issue
Block a user