diff --git a/src/app/cars/[detail]/page.tsx b/src/app/cars/[detail]/page.tsx index 1092605..2d9fd7d 100644 --- a/src/app/cars/[detail]/page.tsx +++ b/src/app/cars/[detail]/page.tsx @@ -5,9 +5,19 @@ import { API_HOST } from '~/constants/apiRelated'; import { Car } from '../page'; -async function getCarDetail(detail: string): Promise { - const res = await fetch(API_HOST + `/api/cars/${detail}`); - return res.json(); +async function getCarDetail(detail: string): Promise { + try { + const res = await fetch(API_HOST + `/api/cars/${detail}`); + + if (!res.ok) { + throw new Error('Failed to fetch data'); + } + + return res.json(); + } catch (e: any) { + console.log(e); + return undefined; + } } export default async function CarsDetailPage({ @@ -19,13 +29,19 @@ export default async function CarsDetailPage({ return (
- driver-image + {car && ( + <> + driver-image - {Object.entries(car).map(([key, value]) => ( -

- {key}: {value} -

- ))} + <> + {Object.entries(car).map(([key, value]) => ( +

+ {key}: {value} +

+ ))} + + + )} diff --git a/src/app/cars/page.tsx b/src/app/cars/page.tsx index 835f8c1..c7da480 100644 --- a/src/app/cars/page.tsx +++ b/src/app/cars/page.tsx @@ -15,10 +15,20 @@ export interface Car { } async function getCars(): Promise { - // You need to set base url in .env as origin of your url - const res = await fetch(API_HOST + '/api/cars'); - await new Promise(resolve => setTimeout(resolve, 4000)); - return res.json(); + try { + // You need to set base url in .env as origin of your url + const res = await fetch(API_HOST + '/api/cars'); + + if (!res.ok) { + throw new Error('Failed to fetch data'); + } + + await new Promise(resolve => setTimeout(resolve, 4000)); + return res.json(); + } catch (e: any) { + console.error(e); + return []; + } } export default async function CarsPage() {