Skip to content

Commit 61ae6fc

Browse files
committed
fix: add graceful error handling for all WordPress API functions
- Add try/catch to searchCategories, searchTags, searchAuthors - Add notFound() handling to posts/[slug] and pages/[slug] routes - Make getPostBySlug and getPageBySlug return undefined on error - Ensures app shows friendly messages instead of server errors when WordPress is unavailable
1 parent c82ef8d commit 61ae6fc

File tree

3 files changed

+54
-20
lines changed

3 files changed

+54
-20
lines changed

app/pages/[slug]/page.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getPageBySlug, getAllPages } from "@/lib/wordpress";
22
import { Section, Container, Prose } from "@/components/craft";
33
import { siteConfig } from "@/site.config";
4+
import { notFound } from "next/navigation";
45

56
import type { Metadata } from "next";
67

@@ -72,6 +73,10 @@ export default async function Page({
7273
const { slug } = await params;
7374
const page = await getPageBySlug(slug);
7475

76+
if (!page) {
77+
notFound();
78+
}
79+
7580
return (
7681
<Section>
7782
<Container>

app/posts/[slug]/page.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { cn } from "@/lib/utils";
1212
import { siteConfig } from "@/site.config";
1313

1414
import Link from "next/link";
15+
import { notFound } from "next/navigation";
1516
import type { Metadata } from "next";
1617

1718
export async function generateStaticParams() {
@@ -69,6 +70,11 @@ export default async function Page({
6970
}) {
7071
const { slug } = await params;
7172
const post = await getPostBySlug(slug);
73+
74+
if (!post) {
75+
notFound();
76+
}
77+
7278
const featuredMedia = post.featured_media
7379
? await getFeaturedMediaById(post.featured_media)
7480
: null;

lib/wordpress.ts

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,14 @@ export async function getPostById(id: number): Promise<Post> {
240240
return wordpressFetch<Post>(`/wp-json/wp/v2/posts/${id}`);
241241
}
242242

243-
export async function getPostBySlug(slug: string): Promise<Post> {
244-
return wordpressFetch<Post[]>("/wp-json/wp/v2/posts", { slug }).then(
245-
(posts) => posts[0]
246-
);
243+
export async function getPostBySlug(slug: string): Promise<Post | undefined> {
244+
try {
245+
const posts = await wordpressFetch<Post[]>("/wp-json/wp/v2/posts", { slug });
246+
return posts[0];
247+
} catch (error) {
248+
console.warn("WordPress unavailable, post not found");
249+
return undefined;
250+
}
247251
}
248252

249253
export async function getAllCategories(): Promise<Category[]> {
@@ -311,10 +315,14 @@ export async function getPageById(id: number): Promise<Page> {
311315
return wordpressFetch<Page>(`/wp-json/wp/v2/pages/${id}`);
312316
}
313317

314-
export async function getPageBySlug(slug: string): Promise<Page> {
315-
return wordpressFetch<Page[]>("/wp-json/wp/v2/pages", { slug }).then(
316-
(pages) => pages[0]
317-
);
318+
export async function getPageBySlug(slug: string): Promise<Page | undefined> {
319+
try {
320+
const pages = await wordpressFetch<Page[]>("/wp-json/wp/v2/pages", { slug });
321+
return pages[0];
322+
} catch (error) {
323+
console.warn("WordPress unavailable, page not found");
324+
return undefined;
325+
}
318326
}
319327

320328
export async function getAllAuthors(): Promise<Author[]> {
@@ -366,24 +374,39 @@ export async function getFeaturedMediaById(id: number): Promise<FeaturedMedia> {
366374
}
367375

368376
export async function searchCategories(query: string): Promise<Category[]> {
369-
return wordpressFetch<Category[]>("/wp-json/wp/v2/categories", {
370-
search: query,
371-
per_page: 100,
372-
});
377+
try {
378+
return await wordpressFetch<Category[]>("/wp-json/wp/v2/categories", {
379+
search: query,
380+
per_page: 100,
381+
});
382+
} catch (error) {
383+
console.warn("WordPress unavailable, returning empty categories");
384+
return [];
385+
}
373386
}
374387

375388
export async function searchTags(query: string): Promise<Tag[]> {
376-
return wordpressFetch<Tag[]>("/wp-json/wp/v2/tags", {
377-
search: query,
378-
per_page: 100,
379-
});
389+
try {
390+
return await wordpressFetch<Tag[]>("/wp-json/wp/v2/tags", {
391+
search: query,
392+
per_page: 100,
393+
});
394+
} catch (error) {
395+
console.warn("WordPress unavailable, returning empty tags");
396+
return [];
397+
}
380398
}
381399

382400
export async function searchAuthors(query: string): Promise<Author[]> {
383-
return wordpressFetch<Author[]>("/wp-json/wp/v2/users", {
384-
search: query,
385-
per_page: 100,
386-
});
401+
try {
402+
return await wordpressFetch<Author[]>("/wp-json/wp/v2/users", {
403+
search: query,
404+
per_page: 100,
405+
});
406+
} catch (error) {
407+
console.warn("WordPress unavailable, returning empty authors");
408+
return [];
409+
}
387410
}
388411

389412
// Function specifically for generateStaticParams - fetches ALL posts

0 commit comments

Comments
 (0)