fix: correct login entry routing and errors

This commit is contained in:
hectorzhao
2026-07-08 18:03:30 +08:00
parent 8144f08652
commit a4a638208e
3 changed files with 44 additions and 3 deletions
+19 -2
View File
@@ -6,6 +6,23 @@ type RequestOptions = RequestInit & {
export const DEFAULT_CLIENT_TENANT_ID = 'tenant-a';
async function readErrorMessage(response: Response) {
const fallback = `请求失败(${response.status}`;
const text = await response.text();
if (!text) return fallback;
try {
const parsed = JSON.parse(text) as { message?: string | string[]; error?: string };
if (Array.isArray(parsed.message)) return parsed.message.join('');
if (parsed.message) return parsed.message;
if (parsed.error) return parsed.error;
} catch {
return text;
}
return text;
}
async function request<T>(path: string, options: RequestOptions = {}): Promise<T> {
const headers = new Headers(options.headers);
headers.set('Content-Type', 'application/json');
@@ -19,7 +36,7 @@ async function request<T>(path: string, options: RequestOptions = {}): Promise<T
}
const response = await fetch(`/api${path}`, { ...options, headers });
if (!response.ok) {
throw new Error(await response.text());
throw new Error(await readErrorMessage(response));
}
return response.json() as Promise<T>;
}
@@ -36,7 +53,7 @@ async function requestBlob(path: string, options: RequestOptions = {}): Promise<
}
const response = await fetch(`/api${path}`, { ...options, headers });
if (!response.ok) {
throw new Error(await response.text());
throw new Error(await readErrorMessage(response));
}
return response.blob();
}