feat: improve error handling in API calls for blog, clinic, and doctor data retrieval

This commit is contained in:
hamed
2026-07-05 15:56:30 +03:30
parent b04bb45ca1
commit f6475d1a7d
3 changed files with 38 additions and 39 deletions
+10 -12
View File
@@ -12,16 +12,13 @@ const FALLBACK_IMG = "https://nobat724.com/assets/images/logo.png";
const API_URL = process.env.NEXT_PUBLIC_API_URL;
const getBlog = cache(async (slug) => {
try {
const res = await fetch(`${API_URL}/api/v1/blog/${slug}`, {
next: { revalidate: 3600, tags: [`blog-${slug}`] },
});
if (!res.ok) return null;
const json = await res.json();
return json?.data?.data ?? null;
} catch {
return null;
}
const res = await fetch(`${API_URL}/api/v1/blog/${slug}`, {
next: { revalidate: 3600, tags: [`blog-${slug}`] },
});
if (res.status === 404 || res.status === 400) return null;
if (!res.ok) throw new Error(`Failed to fetch blog: ${res.status}`);
const json = await res.json();
return json?.data?.data ?? null;
});
export async function generateMetadata({ params }) {
@@ -29,9 +26,10 @@ export async function generateMetadata({ params }) {
const { matchedCity } = await getStateInfo();
const siteName = matchedCity?.site_name || "نوبت 724";
const blog = await getBlog(slug);
if (!blog) notFound();
try {
const blog = await getBlog(slug);
if (!blog) return {};
const title = `${blog.title} | ${siteName}`;
const rawText = (blog.summary || blog.body || "")