fix: 统一异常处理
This commit is contained in:
1 parent
4daf8dfcaa
commit
0c5cfcaa4a
21 files changed
+2124
-4218
No files matched your search
+108
-193
@@ -1,4 +1,5 @@
|
||||
import { getAuthHeaders } from '../utils/auth';
|
||||
import { BusinessError, requestJson } from './client';
|
||||
|
||||
export interface KbGroup {
|
||||
id: number;
|
||||
@@ -116,11 +117,14 @@ export interface KbListResponse<T> {
|
||||
status?: number;
|
||||
}
|
||||
|
||||
const auth = () => ({ headers: getAuthHeaders() });
|
||||
const jsonAuth = () => ({ headers: { 'Content-Type': 'application/json', ...getAuthHeaders() } });
|
||||
|
||||
const kbErrorMessage = (e: unknown, fallback: string) => (e as Error).message || fallback;
|
||||
|
||||
const getJson = async <T>(path: string, fallback: T): Promise<T> => {
|
||||
try {
|
||||
const res = await fetch(path, { headers: getAuthHeaders() });
|
||||
if (!res.ok) return fallback;
|
||||
return (await res.json()) as T;
|
||||
return await requestJson<T>(path, auth());
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
@@ -128,19 +132,16 @@ const getJson = async <T>(path: string, fallback: T): Promise<T> => {
|
||||
|
||||
const getJsonWithStatus = async <T>(path: string, fallback: T): Promise<KbListResponse<T>> => {
|
||||
try {
|
||||
const res = await fetch(path, { headers: getAuthHeaders() });
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
return {
|
||||
ok: false,
|
||||
data: fallback,
|
||||
error: data?.error || '知识库接口请求失败',
|
||||
status: res.status,
|
||||
};
|
||||
}
|
||||
return { ok: true, data: (await res.json()) as T, status: res.status };
|
||||
} catch {
|
||||
return { ok: false, data: fallback, error: '知识库接口暂未就绪,请稍后再试' };
|
||||
const data = await requestJson<T>(path, auth());
|
||||
return { ok: true, data };
|
||||
} catch (e) {
|
||||
const err = e as BusinessError;
|
||||
return {
|
||||
ok: false,
|
||||
data: fallback,
|
||||
error: kbErrorMessage(e, '知识库接口请求失败'),
|
||||
status: err.status,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
@@ -165,21 +166,14 @@ export const createKbGroup = async (payload: {
|
||||
description?: string;
|
||||
}): Promise<{ ok: boolean; error?: string; group?: KbGroup }> => {
|
||||
try {
|
||||
const res = await fetch('/api/kb/groups', {
|
||||
const group = await requestJson<KbGroup>('/api/kb/groups', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...getAuthHeaders(),
|
||||
},
|
||||
...jsonAuth(),
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
return { ok: false, error: data?.error || '创建知识库分组失败' };
|
||||
}
|
||||
return { ok: true, group: (await res.json()) as KbGroup };
|
||||
} catch {
|
||||
return { ok: false, error: '知识库接口暂未就绪,请稍后再试' };
|
||||
return { ok: true, group };
|
||||
} catch (e) {
|
||||
return { ok: false, error: kbErrorMessage(e, '创建知识库分组失败') };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -199,21 +193,14 @@ export const updateKbDifySettings = async (
|
||||
payload: Partial<KbDifySettings>
|
||||
): Promise<{ ok: boolean; error?: string; settings?: KbDifySettings }> => {
|
||||
try {
|
||||
const res = await fetch('/api/kb/dify/settings', {
|
||||
const settings = await requestJson<KbDifySettings>('/api/kb/dify/settings', {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...getAuthHeaders(),
|
||||
},
|
||||
...jsonAuth(),
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
return { ok: false, error: data?.error || '保存 Dify 设置失败' };
|
||||
}
|
||||
return { ok: true, settings: (await res.json()) as KbDifySettings };
|
||||
} catch {
|
||||
return { ok: false, error: '知识库接口暂未就绪,请稍后再试' };
|
||||
return { ok: true, settings };
|
||||
} catch (e) {
|
||||
return { ok: false, error: kbErrorMessage(e, '保存 Dify 设置失败') };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -225,18 +212,13 @@ export const runKbJob = async (
|
||||
jobId: number | string
|
||||
): Promise<{ ok: boolean; error?: string; job?: KbJob; run_result?: string }> => {
|
||||
try {
|
||||
const res = await fetch(`/api/kb/jobs/${jobId}/run`, {
|
||||
const job = await requestJson<KbJob & { run_result?: string }>(`/api/kb/jobs/${jobId}/run`, {
|
||||
method: 'POST',
|
||||
headers: getAuthHeaders(),
|
||||
...auth(),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
return { ok: false, error: data?.error || '执行任务失败' };
|
||||
}
|
||||
const job = (await res.json()) as KbJob & { run_result?: string };
|
||||
return { ok: true, job, run_result: job.run_result };
|
||||
} catch {
|
||||
return { ok: false, error: '知识库接口暂未就绪,请稍后再试' };
|
||||
} catch (e) {
|
||||
return { ok: false, error: kbErrorMessage(e, '执行任务失败') };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -245,21 +227,14 @@ export const createKbDocument = async (
|
||||
payload: { title: string; description?: string }
|
||||
): Promise<{ ok: boolean; error?: string; document?: KbDocumentItem }> => {
|
||||
try {
|
||||
const res = await fetch(`/api/kb/groups/${groupId}/files`, {
|
||||
const document = await requestJson<KbDocumentItem>(`/api/kb/groups/${groupId}/files`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...getAuthHeaders(),
|
||||
},
|
||||
...jsonAuth(),
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
return { ok: false, error: data?.error || '创建文档失败' };
|
||||
}
|
||||
return { ok: true, document: (await res.json()) as KbDocumentItem };
|
||||
} catch {
|
||||
return { ok: false, error: '知识库接口暂未就绪,请稍后再试' };
|
||||
return { ok: true, document };
|
||||
} catch (e) {
|
||||
return { ok: false, error: kbErrorMessage(e, '创建文档失败') };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -268,21 +243,14 @@ export const updateKbDocument = async (
|
||||
payload: { title: string; description?: string; status?: string }
|
||||
): Promise<{ ok: boolean; error?: string; document?: KbDocumentItem }> => {
|
||||
try {
|
||||
const res = await fetch(`/api/kb/documents/${documentId}`, {
|
||||
const document = await requestJson<KbDocumentItem>(`/api/kb/documents/${documentId}`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...getAuthHeaders(),
|
||||
},
|
||||
...jsonAuth(),
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
return { ok: false, error: data?.error || '更新文档失败' };
|
||||
}
|
||||
return { ok: true, document: (await res.json()) as KbDocumentItem };
|
||||
} catch {
|
||||
return { ok: false, error: '知识库接口暂未就绪,请稍后再试' };
|
||||
return { ok: true, document };
|
||||
} catch (e) {
|
||||
return { ok: false, error: kbErrorMessage(e, '更新文档失败') };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -290,17 +258,10 @@ export const deleteKbDocument = async (
|
||||
documentId: number
|
||||
): Promise<{ ok: boolean; error?: string }> => {
|
||||
try {
|
||||
const res = await fetch(`/api/kb/documents/${documentId}`, {
|
||||
method: 'DELETE',
|
||||
headers: getAuthHeaders(),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
return { ok: false, error: data?.error || '删除文档失败' };
|
||||
}
|
||||
await requestJson(`/api/kb/documents/${documentId}`, { method: 'DELETE', ...auth() });
|
||||
return { ok: true };
|
||||
} catch {
|
||||
return { ok: false, error: '知识库接口暂未就绪,请稍后再试' };
|
||||
} catch (e) {
|
||||
return { ok: false, error: kbErrorMessage(e, '删除文档失败') };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -318,8 +279,15 @@ export const getKbDocumentVersionSourceFile = async (
|
||||
headers: getAuthHeaders(),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
return { ok: false, error: data?.error || '读取原始文件失败' };
|
||||
let message = '读取原始文件失败';
|
||||
try {
|
||||
const data = await res.json();
|
||||
if (typeof data?.error === 'string' && data.error.trim()) message = data.error;
|
||||
else if (typeof data?.message === 'string' && data.message.trim()) message = data.message;
|
||||
} catch {
|
||||
// ignore non-json error body
|
||||
}
|
||||
return { ok: false, error: message };
|
||||
}
|
||||
const disposition = res.headers.get('content-disposition') || '';
|
||||
const filenameMatch = disposition.match(/filename\*=UTF-8''([^;]+)|filename="?([^"]+)"?/i);
|
||||
@@ -345,16 +313,10 @@ export const getKbDocumentMarkdown = async (
|
||||
documentId: number
|
||||
): Promise<{ ok: boolean; error?: string; data?: KbDocumentMarkdown }> => {
|
||||
try {
|
||||
const res = await fetch(`/api/kb/documents/${documentId}/markdown`, {
|
||||
headers: getAuthHeaders(),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
return { ok: false, error: data?.error || '读取清洗后的 Markdown 失败' };
|
||||
}
|
||||
return { ok: true, data: (await res.json()) as KbDocumentMarkdown };
|
||||
} catch {
|
||||
return { ok: false, error: '知识库接口暂未就绪,请稍后再试' };
|
||||
const data = await requestJson<KbDocumentMarkdown>(`/api/kb/documents/${documentId}/markdown`, auth());
|
||||
return { ok: true, data };
|
||||
} catch (e) {
|
||||
return { ok: false, error: kbErrorMessage(e, '读取清洗后的 Markdown 失败') };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -363,21 +325,14 @@ export const updateKbDocumentMarkdown = async (
|
||||
payload: { content: string }
|
||||
): Promise<{ ok: boolean; error?: string; data?: KbDocumentMarkdown }> => {
|
||||
try {
|
||||
const res = await fetch(`/api/kb/documents/${documentId}/markdown`, {
|
||||
const data = await requestJson<KbDocumentMarkdown>(`/api/kb/documents/${documentId}/markdown`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...getAuthHeaders(),
|
||||
},
|
||||
...jsonAuth(),
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
return { ok: false, error: data?.error || '保存 Markdown 失败' };
|
||||
}
|
||||
return { ok: true, data: (await res.json()) as KbDocumentMarkdown };
|
||||
} catch {
|
||||
return { ok: false, error: '知识库接口暂未就绪,请稍后再试' };
|
||||
return { ok: true, data };
|
||||
} catch (e) {
|
||||
return { ok: false, error: kbErrorMessage(e, '保存 Markdown 失败') };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -388,22 +343,22 @@ export const uploadKbDocumentVersion = async (
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
|
||||
const res = await fetch(`/api/kb/documents/${documentId}/upload`, {
|
||||
const version = await requestJson<KbDocumentVersionItem>(`/api/kb/documents/${documentId}/upload`, {
|
||||
method: 'POST',
|
||||
headers: getAuthHeaders(),
|
||||
...auth(),
|
||||
body: formData,
|
||||
});
|
||||
if (!res.ok) {
|
||||
if (res.status === 413) {
|
||||
return { ok: false, error: '文件过大,超过当前上传限制,请联系管理员放宽限制后重试', status: res.status };
|
||||
}
|
||||
const data = await res.json().catch(() => ({}));
|
||||
return { ok: false, error: data?.error || '上传文档版本失败', status: res.status };
|
||||
return { ok: true, version };
|
||||
} catch (e) {
|
||||
const err = e as BusinessError;
|
||||
if (err.status === 413) {
|
||||
return {
|
||||
ok: false,
|
||||
error: '文件过大,超过当前上传限制,请联系管理员放宽限制后重试',
|
||||
status: 413,
|
||||
};
|
||||
}
|
||||
return { ok: true, version: (await res.json()) as KbDocumentVersionItem, status: res.status };
|
||||
} catch {
|
||||
return { ok: false, error: '知识库接口暂未就绪,请稍后再试' };
|
||||
return { ok: false, error: kbErrorMessage(e, '上传文档版本失败'), status: err.status };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -412,21 +367,15 @@ export const createKbPathUploadJob = async (
|
||||
payload: { total_files: number; selected_paths: string[] }
|
||||
): Promise<{ ok: boolean; error?: string; job?: KbJob; status?: number }> => {
|
||||
try {
|
||||
const res = await fetch(`/api/kb/groups/${groupId}/path-upload-jobs`, {
|
||||
const job = await requestJson<KbJob>(`/api/kb/groups/${groupId}/path-upload-jobs`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...getAuthHeaders(),
|
||||
},
|
||||
...jsonAuth(),
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok) {
|
||||
return { ok: false, error: data?.error || '创建路径上传任务失败', status: res.status };
|
||||
}
|
||||
return { ok: true, job: data as KbJob, status: res.status };
|
||||
} catch {
|
||||
return { ok: false, error: '知识库接口暂未就绪,请稍后再试' };
|
||||
return { ok: true, job };
|
||||
} catch (e) {
|
||||
const err = e as BusinessError;
|
||||
return { ok: false, error: kbErrorMessage(e, '创建路径上传任务失败'), status: err.status };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -439,19 +388,14 @@ export const uploadKbPathJobFile = async (
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
formData.append('relative_path', relativePath);
|
||||
|
||||
const res = await fetch(`/api/kb/path-upload-jobs/${jobId}/files`, {
|
||||
const job = await requestJson<KbJob>(`/api/kb/path-upload-jobs/${jobId}/files`, {
|
||||
method: 'POST',
|
||||
headers: getAuthHeaders(),
|
||||
...auth(),
|
||||
body: formData,
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok) {
|
||||
return { ok: false, error: data?.error || '上传路径文件失败' };
|
||||
}
|
||||
return { ok: true, job: data as KbJob };
|
||||
} catch {
|
||||
return { ok: false, error: '知识库接口暂未就绪,请稍后再试' };
|
||||
return { ok: true, job };
|
||||
} catch (e) {
|
||||
return { ok: false, error: kbErrorMessage(e, '上传路径文件失败') };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -459,17 +403,13 @@ export const finalizeKbPathUploadJob = async (
|
||||
jobId: number | string,
|
||||
): Promise<{ ok: boolean; error?: string; job?: KbJob }> => {
|
||||
try {
|
||||
const res = await fetch(`/api/kb/path-upload-jobs/${jobId}/finalize`, {
|
||||
const job = await requestJson<KbJob>(`/api/kb/path-upload-jobs/${jobId}/finalize`, {
|
||||
method: 'POST',
|
||||
headers: getAuthHeaders(),
|
||||
...auth(),
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok) {
|
||||
return { ok: false, error: data?.error || '提交路径上传任务失败' };
|
||||
}
|
||||
return { ok: true, job: data as KbJob };
|
||||
} catch {
|
||||
return { ok: false, error: '知识库接口暂未就绪,请稍后再试' };
|
||||
return { ok: true, job };
|
||||
} catch (e) {
|
||||
return { ok: false, error: kbErrorMessage(e, '提交路径上传任务失败') };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -478,21 +418,15 @@ export const retryFailedKbPathUploadJob = async (
|
||||
payload?: { relative_paths?: string[] },
|
||||
): Promise<{ ok: boolean; error?: string; job?: KbJob; status?: number }> => {
|
||||
try {
|
||||
const res = await fetch(`/api/kb/path-upload-jobs/${jobId}/retry-failed`, {
|
||||
const job = await requestJson<KbJob>(`/api/kb/path-upload-jobs/${jobId}/retry-failed`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...getAuthHeaders(),
|
||||
},
|
||||
...jsonAuth(),
|
||||
body: JSON.stringify(payload || {}),
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok) {
|
||||
return { ok: false, error: data?.error || '重试失败文件失败', status: res.status };
|
||||
}
|
||||
return { ok: true, job: data as KbJob, status: res.status };
|
||||
} catch {
|
||||
return { ok: false, error: '知识库接口暂未就绪,请稍后再试' };
|
||||
return { ok: true, job };
|
||||
} catch (e) {
|
||||
const err = e as BusinessError;
|
||||
return { ok: false, error: kbErrorMessage(e, '重试失败文件失败'), status: err.status };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -500,18 +434,13 @@ export const syncKbDocumentToDify = async (
|
||||
documentId: number
|
||||
): Promise<{ ok: boolean; error?: string; job?: KbJob; run_result?: string }> => {
|
||||
try {
|
||||
const res = await fetch(`/api/kb/documents/${documentId}/sync`, {
|
||||
const job = await requestJson<KbJob & { run_result?: string }>(`/api/kb/documents/${documentId}/sync`, {
|
||||
method: 'POST',
|
||||
headers: getAuthHeaders(),
|
||||
...auth(),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
return { ok: false, error: data?.error || data?.error_message || data?.run_result || '同步到 Dify 失败' };
|
||||
}
|
||||
const job = (await res.json()) as KbJob & { run_result?: string };
|
||||
return { ok: true, job, run_result: job.run_result };
|
||||
} catch {
|
||||
return { ok: false, error: '知识库接口暂未就绪,请稍后再试' };
|
||||
} catch (e) {
|
||||
return { ok: false, error: kbErrorMessage(e, '同步到 Dify 失败') };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -520,21 +449,14 @@ export const updateKbGroup = async (
|
||||
payload: { name: string; description?: string }
|
||||
): Promise<{ ok: boolean; error?: string; group?: KbGroup }> => {
|
||||
try {
|
||||
const res = await fetch(`/api/kb/groups/${groupId}`, {
|
||||
const group = await requestJson<KbGroup>(`/api/kb/groups/${groupId}`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...getAuthHeaders(),
|
||||
},
|
||||
...jsonAuth(),
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
return { ok: false, error: data?.error || '更新知识库分组失败' };
|
||||
}
|
||||
return { ok: true, group: (await res.json()) as KbGroup };
|
||||
} catch {
|
||||
return { ok: false, error: '知识库接口暂未就绪,请稍后再试' };
|
||||
return { ok: true, group };
|
||||
} catch (e) {
|
||||
return { ok: false, error: kbErrorMessage(e, '更新知识库分组失败') };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -542,16 +464,9 @@ export const deleteKbGroup = async (
|
||||
groupId: number
|
||||
): Promise<{ ok: boolean; error?: string }> => {
|
||||
try {
|
||||
const res = await fetch(`/api/kb/groups/${groupId}`, {
|
||||
method: 'DELETE',
|
||||
headers: getAuthHeaders(),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
return { ok: false, error: data?.error || '删除知识库分组失败' };
|
||||
}
|
||||
await requestJson(`/api/kb/groups/${groupId}`, { method: 'DELETE', ...auth() });
|
||||
return { ok: true };
|
||||
} catch {
|
||||
return { ok: false, error: '知识库接口暂未就绪,请稍后再试' };
|
||||
} catch (e) {
|
||||
return { ok: false, error: kbErrorMessage(e, '删除知识库分组失败') };
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user