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 API_URL = process.env.NEXT_PUBLIC_API_URL;
const getBlog = cache(async (slug) => { const getBlog = cache(async (slug) => {
try { const res = await fetch(`${API_URL}/api/v1/blog/${slug}`, {
const res = await fetch(`${API_URL}/api/v1/blog/${slug}`, { next: { revalidate: 3600, tags: [`blog-${slug}`] },
next: { revalidate: 3600, tags: [`blog-${slug}`] }, });
}); if (res.status === 404 || res.status === 400) return null;
if (!res.ok) return null; if (!res.ok) throw new Error(`Failed to fetch blog: ${res.status}`);
const json = await res.json(); const json = await res.json();
return json?.data?.data ?? null; return json?.data?.data ?? null;
} catch {
return null;
}
}); });
export async function generateMetadata({ params }) { export async function generateMetadata({ params }) {
@@ -29,9 +26,10 @@ export async function generateMetadata({ params }) {
const { matchedCity } = await getStateInfo(); const { matchedCity } = await getStateInfo();
const siteName = matchedCity?.site_name || "نوبت 724"; const siteName = matchedCity?.site_name || "نوبت 724";
const blog = await getBlog(slug);
if (!blog) notFound();
try { try {
const blog = await getBlog(slug);
if (!blog) return {};
const title = `${blog.title} | ${siteName}`; const title = `${blog.title} | ${siteName}`;
const rawText = (blog.summary || blog.body || "") const rawText = (blog.summary || blog.body || "")
+10 -12
View File
@@ -11,16 +11,13 @@ import { imageUrl } from "@/helper";
const API_URL = process.env.NEXT_PUBLIC_API_URL; const API_URL = process.env.NEXT_PUBLIC_API_URL;
const getClinic = cache(async (slug) => { const getClinic = cache(async (slug) => {
try { const res = await fetch(`${API_URL}/api/v1/clinic/${slug}`, {
const res = await fetch(`${API_URL}/api/v1/clinic/${slug}`, { next: { revalidate: 3600, tags: [`clinic-${slug}`] },
next: { revalidate: 3600, tags: [`clinic-${slug}`] }, });
}); if (res.status === 404 || res.status === 400) return null;
if (!res.ok) return null; if (!res.ok) throw new Error(`Failed to fetch clinic: ${res.status}`);
const json = await res.json(); const json = await res.json();
return json?.data?.data ?? null; return json?.data?.data ?? null;
} catch {
return null;
}
}); });
export async function generateMetadata({ params }) { export async function generateMetadata({ params }) {
@@ -28,9 +25,10 @@ export async function generateMetadata({ params }) {
const { matchedCity } = await getStateInfo(); const { matchedCity } = await getStateInfo();
const siteName = matchedCity?.site_name || "نوبت 724"; const siteName = matchedCity?.site_name || "نوبت 724";
const clinic = await getClinic(slug);
if (!clinic) notFound();
try { try {
const clinic = await getClinic(slug);
if (!clinic) return {};
const title = `${clinic.title} | ${siteName}`; const title = `${clinic.title} | ${siteName}`;
const description = `رزرو نوبت و اطلاعات ${clinic.title}. مشاهده لیست پزشکان و خدمات درمانی موجود.`; const description = `رزرو نوبت و اطلاعات ${clinic.title}. مشاهده لیست پزشکان و خدمات درمانی موجود.`;
+18 -15
View File
@@ -11,16 +11,13 @@ const API_URL = process.env.NEXT_PUBLIC_API_URL;
const FALLBACK_IMG = "https://nobat724.com/assets/images/logo.png"; const FALLBACK_IMG = "https://nobat724.com/assets/images/logo.png";
const getDoctor = cache(async (slug) => { const getDoctor = cache(async (slug) => {
try { const res = await fetch(`${API_URL}/api/v1/doctor/${slug}`, {
const res = await fetch(`${API_URL}/api/v1/doctor/${slug}`, { next: { revalidate: 3600, tags: [`doctor-${slug}`] },
next: { revalidate: 3600, tags: [`doctor-${slug}`] }, });
}); if (res.status === 404 || res.status === 400) return null;
if (!res.ok) return null; if (!res.ok) throw new Error(`Failed to fetch doctor: ${res.status}`);
const json = await res.json(); const json = await res.json();
return json?.data?.data ?? null; return json?.data?.data ?? null;
} catch {
return null;
}
}); });
const getDoctorAddresses = cache(async (doctorId) => { const getDoctorAddresses = cache(async (doctorId) => {
@@ -41,15 +38,21 @@ export async function generateMetadata({ params }) {
const { matchedCity } = await getStateInfo(); const { matchedCity } = await getStateInfo();
const siteName = matchedCity?.site_name || "نوبت 724"; const siteName = matchedCity?.site_name || "نوبت 724";
const doctor = await getDoctor(slug);
if (!doctor) notFound();
try { try {
const doctor = await getDoctor(slug);
if (!doctor) return {};
const specialtyNames = doctor.specialties?.map((s) => s.name).join(" و ") || ""; const specialtyNames = doctor.specialties?.map((s) => s.name).join(" و ") || "";
const title = `دکتر ${doctor.name}${specialtyNames ? " | " + specialtyNames : ""} | ${siteName}`; const title = `دکتر ${doctor.name}${specialtyNames ? " | " + specialtyNames : ""} | ${siteName}`;
const description = const detailText = (doctor.detail || "")
doctor.detail || .replace(/<[^>]*>/g, "")
`رزرو نوبت آنلاین دکتر ${doctor.name}${specialtyNames ? " متخصص " + specialtyNames : ""}${doctor.address ? " | " + doctor.address : ""}`.trim(); .replace(/\s+/g, " ")
.trim();
const description = (
detailText ||
`رزرو نوبت آنلاین دکتر ${doctor.name}${specialtyNames ? " متخصص " + specialtyNames : ""}${doctor.address ? " | " + doctor.address : ""}`.trim()
).slice(0, 160);
const image = imageUrl(doctor.img?.[0]?.url) || FALLBACK_IMG; const image = imageUrl(doctor.img?.[0]?.url) || FALLBACK_IMG;
return { return {