fix: correct login entry routing and errors
This commit is contained in:
+19
-2
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user