Skip to content

Commit 7c0a9e1

Browse files
committed
fix(website): 添加所有 API 函数的错误处理
为所有可能抛出错误的 API 函数添加 try-catch: - getPublishedContents: 返回空数据 - getContentBySlug: 抛出错误 - getTags: 返回空数组 - getCrProducts: 返回空数据 - getProductByCode: 抛出错误 这样在 CI 构建时,即使后端不可用也能成功预渲染所有页面。 验证:pnpm build 成功通过,所有 10 个页面预渲染完成
1 parent 07bf2c1 commit 7c0a9e1

1 file changed

Lines changed: 68 additions & 43 deletions

File tree

website/lib/api.ts

Lines changed: 68 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -77,32 +77,42 @@ export async function getPublishedContents(params?: {
7777
keyword?: string;
7878
tagId?: string;
7979
}): Promise<PaginatedResponse<CmsContent>> {
80-
const queryParams = new URLSearchParams();
81-
if (params?.contentType) queryParams.set("contentType", params.contentType);
82-
if (params?.page) queryParams.set("page", params.page.toString());
83-
if (params?.pageSize) queryParams.set("pageSize", params.pageSize.toString());
84-
if (params?.keyword) queryParams.set("keyword", params.keyword);
85-
if (params?.tagId) queryParams.set("tagId", params.tagId);
86-
87-
const res = await fetch(
88-
`${API_BASE_URL}/public/cms/contents?${queryParams}`,
89-
{
90-
next: { revalidate: 3600 }, // ISR: 每小时重新生成
91-
}
92-
);
93-
94-
if (!res.ok) throw new Error("Failed to fetch contents");
95-
return res.json();
80+
try {
81+
const queryParams = new URLSearchParams();
82+
if (params?.contentType) queryParams.set("contentType", params.contentType);
83+
if (params?.page) queryParams.set("page", params.page.toString());
84+
if (params?.pageSize) queryParams.set("pageSize", params.pageSize.toString());
85+
if (params?.keyword) queryParams.set("keyword", params.keyword);
86+
if (params?.tagId) queryParams.set("tagId", params.tagId);
87+
88+
const res = await fetch(
89+
`${API_BASE_URL}/public/cms/contents?${queryParams}`,
90+
{
91+
next: { revalidate: 3600 }, // ISR: 每小时重新生成
92+
}
93+
);
94+
95+
if (!res.ok) throw new Error("Failed to fetch contents");
96+
return res.json();
97+
} catch {
98+
// 构建时后端可能不可用,返回空数据
99+
return { data: [], total: 0, page: 1, pageSize: 10, totalPages: 0 };
100+
}
96101
}
97102

98103
// 根据 slug 获取内容详情
99104
export async function getContentBySlug(slug: string): Promise<CmsContent> {
100-
const res = await fetch(`${API_BASE_URL}/public/cms/contents/${slug}`, {
101-
next: { revalidate: 3600 },
102-
});
105+
try {
106+
const res = await fetch(`${API_BASE_URL}/public/cms/contents/${slug}`, {
107+
next: { revalidate: 3600 },
108+
});
103109

104-
if (!res.ok) throw new Error("Content not found");
105-
return res.json();
110+
if (!res.ok) throw new Error("Content not found");
111+
return res.json();
112+
} catch {
113+
// 构建时后端可能不可用,抛出错误让页面处理
114+
throw new Error("Content not found");
115+
}
106116
}
107117

108118
// 便捷函数:获取文章
@@ -167,12 +177,17 @@ export async function getPageElement(slug: string): Promise<CmsContent | null> {
167177
export async function getTags(): Promise<
168178
Array<{ id: string; name: string; color?: string }>
169179
> {
170-
const res = await fetch(`${API_BASE_URL}/public/cms/tags`, {
171-
next: { revalidate: 86400 }, // 标签变更较少,缓存 24 小时
172-
});
180+
try {
181+
const res = await fetch(`${API_BASE_URL}/public/cms/tags`, {
182+
next: { revalidate: 86400 }, // 标签变更较少,缓存 24 小时
183+
});
173184

174-
if (!res.ok) throw new Error("Failed to fetch tags");
175-
return res.json();
185+
if (!res.ok) throw new Error("Failed to fetch tags");
186+
return res.json();
187+
} catch {
188+
// 构建时后端可能不可用,返回空数组
189+
return [];
190+
}
176191
}
177192

178193
// ==================== 页面管理 API ====================
@@ -260,26 +275,36 @@ export async function getCrProducts(params?: {
260275
pageSize?: number;
261276
keyword?: string;
262277
}): Promise<PaginatedResponse<Product>> {
263-
const queryParams = new URLSearchParams();
264-
if (params?.page) queryParams.set("page", params.page.toString());
265-
if (params?.pageSize) queryParams.set("pageSize", params.pageSize.toString());
266-
if (params?.keyword) queryParams.set("keyword", params.keyword);
267-
268-
const res = await fetch(
269-
`${API_BASE_URL}/public/products?${queryParams}`,
270-
{ next: { revalidate: 7200 } } // 产品缓存 2 小时
271-
);
272-
273-
if (!res.ok) throw new Error("Failed to fetch products");
274-
return res.json();
278+
try {
279+
const queryParams = new URLSearchParams();
280+
if (params?.page) queryParams.set("page", params.page.toString());
281+
if (params?.pageSize) queryParams.set("pageSize", params.pageSize.toString());
282+
if (params?.keyword) queryParams.set("keyword", params.keyword);
283+
284+
const res = await fetch(
285+
`${API_BASE_URL}/public/products?${queryParams}`,
286+
{ next: { revalidate: 7200 } } // 产品缓存 2 小时
287+
);
288+
289+
if (!res.ok) throw new Error("Failed to fetch products");
290+
return res.json();
291+
} catch {
292+
// 构建时后端可能不可用,返回空数据
293+
return { data: [], total: 0, page: 1, pageSize: 10, totalPages: 0 };
294+
}
275295
}
276296

277297
// 根据 code 获取产品详情
278298
export async function getProductByCode(code: string): Promise<Product> {
279-
const res = await fetch(`${API_BASE_URL}/public/products/${code}`, {
280-
next: { revalidate: 7200 },
281-
});
299+
try {
300+
const res = await fetch(`${API_BASE_URL}/public/products/${code}`, {
301+
next: { revalidate: 7200 },
302+
});
282303

283-
if (!res.ok) throw new Error("Product not found");
284-
return res.json();
304+
if (!res.ok) throw new Error("Product not found");
305+
return res.json();
306+
} catch {
307+
// 构建时后端可能不可用,抛出错误让页面处理
308+
throw new Error("Product not found");
309+
}
285310
}

0 commit comments

Comments
 (0)