fix: harden online validation flows
This commit is contained in:
@@ -55,7 +55,20 @@ async function request<T>(
|
||||
Object.assign(headers, options.headers as Record<string, string>);
|
||||
if (token) headers['Authorization'] = `Bearer ${token}`;
|
||||
|
||||
const res = await fetch(`${BASE_URL}${path}`, { ...options, headers });
|
||||
const url = `${BASE_URL}${path}`;
|
||||
const res = await fetch(url, { ...options, headers });
|
||||
const contentType = res.headers.get('content-type') || '';
|
||||
if (!contentType.includes('application/json')) {
|
||||
const text = await res.text();
|
||||
const apiError = new Error(
|
||||
`接口返回非 JSON(${res.status}),请刷新后重试;如果仍出现,请联系管理员检查后端代理。`
|
||||
) as Error & { code?: number; status?: number; responseText?: string; url?: string };
|
||||
apiError.code = res.status || 50001;
|
||||
apiError.status = res.status;
|
||||
apiError.responseText = text.slice(0, 160);
|
||||
apiError.url = url;
|
||||
throw apiError;
|
||||
}
|
||||
const body: ApiResponse<T> | ApiError = await res.json();
|
||||
|
||||
if ('code' in body && body.code === ERROR_CODES.SUCCESS) {
|
||||
|
||||
Reference in New Issue
Block a user