Skip to content

Commit

Permalink
feat: Enhance error handling for JSON file reading and validate categ…
Browse files Browse the repository at this point in the history
…ory and slug parameters
  • Loading branch information
SivaramPg committed Dec 21, 2024
1 parent 6f895b2 commit 17d7e70
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
38 changes: 30 additions & 8 deletions src/app/[category]/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ApiEndpointElement from "@/components/ApiEndpointElement"
import Breadcrumbs from "@/components/Breadcrumbs"
import RequestDisplayElement from "@/components/RequestDisplayElement"
import ResponseDisplayElement from "@/components/ResponseDisplayElement"
import { notFound } from "next/navigation"

export const dynamic = "force-static"

Expand Down Expand Up @@ -35,15 +36,19 @@ export async function generateStaticParams() {
}

async function getCategorySlugJson(category: string, slug: string) {
const jsonFile = await readFile(
path.resolve(
__dirname,
`../../../../../public/api/${category}/${slug}.json`,
),
)
const json = await JSON.parse(jsonFile.toString())
try {
const jsonFile = await readFile(
path.resolve(
__dirname,
`../../../../../public/api/${category}/${slug}.json`,
),
)
const json = await JSON.parse(jsonFile.toString())

return json
return json
} catch (error) {
notFound()
}
}

export default async function Page({
Expand All @@ -53,6 +58,23 @@ export default async function Page({
}) {
const { category, slug } = await params

if (
![
"films",
"people",
"planets",
"species",
"starships",
"vehicles",
].includes(category) ||
+slug <= 0 ||
+slug > 100 ||
Number.isNaN(+slug) ||
!Number.isInteger(+slug)
) {
return notFound()
}

const data = await getCategorySlugJson(category, slug)

return (
Expand Down
28 changes: 23 additions & 5 deletions src/app/[category]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SpriteIcon, { Icons } from "@/components/SpriteIcon"
import { resolve } from "node:path"
import RequestDisplayElement from "@/components/RequestDisplayElement"
import { LinkPill } from "@/components/link-pill"
import { notFound } from "next/navigation"

export const dynamic = "force-static"

Expand All @@ -30,12 +31,16 @@ export async function generateStaticParams() {
}

async function getCategoryAllJson(category: string) {
const jsonFile = await readFile(
resolve(__dirname, `../../../../public/api/${category}/all.json`),
)
const json = await JSON.parse(jsonFile.toString())
try {
const jsonFile = await readFile(
resolve(__dirname, `../../../../public/api/${category}/all.json`),
)
const json = await JSON.parse(jsonFile.toString())

return json
return json
} catch (error) {
notFound()
}
}

export default async function Page({
Expand All @@ -45,6 +50,19 @@ export default async function Page({
}) {
const { category } = await params

if (
![
"films",
"people",
"planets",
"species",
"starships",
"vehicles",
].includes(category)
) {
return notFound()
}

const data = await getCategoryAllJson(category)

return (
Expand Down
4 changes: 2 additions & 2 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "./globals.css"

import type { Metadata } from "next"
import { Poppins } from "next/font/google"
import { Inter } from "next/font/google"
import Image from "next/image"

import QuickLinks from "@/components/QuickLinks"
Expand All @@ -12,7 +12,7 @@ import { AllSystemsNormal } from "./_components/all-systems-normal"
import NewNavbar from "./_components/new-navbar"
import { Providers } from "./providers"

const syne = Poppins({
const syne = Inter({
subsets: ["latin"],
display: "swap",
preload: true,
Expand Down

0 comments on commit 17d7e70

Please sign in to comment.