url에서 게시글 id를 조작해 일부러 404 페이지를 유도할 때
조작된 id가 짧거나 잘못된 타입일 경우 다음과 같은 에러가 나는 것 같습니다.
Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer
아마 몽고디비의 ObjectId에서 만들어내는 에러인 것 같아서 try - catch 문으로 임시 해결해 봤습니다.
이 방법이 적절한 건가요?
import { connectDB } from "@/util/db";
import { ObjectId } from "mongodb";
import Comment from "@/components/Comment";
import { getServerSession } from "next-auth";
import { authOptions } from "@/pages/api/auth/[...nextauth]";
import GaeChuButton from "@/components/GaeChuButton";
import style from "./Detail.module.scss";
import common from "@/app/common.module.scss";
export default async function Detail({ params }) {
const session = await getServerSession(authOptions);
const db = (await connectDB).db("next-inside");
let article;
try {
article = await db
.collection("article")
.findOne({ _id: new ObjectId(params.articleId) });
if (!article) {
return <div>존재하지 않는 게시글입니다.</div>;
}
} catch {
return <div>존재하지 않는 게시글입니다.</div>;
}
return (
<div className={style.Detail}>
<div
className={`${style.GallView} ${common.CutCard} ${common["CutCard--Bottom"]}`}
>
<div className={style.StickyWrap}>
<header className={style.GallViewHead}>
<h2 className={style.GallViewTitle}>{article.title}</h2>
<p className={style.GallViewAuthor}>{article.author}</p>
</header>
<p className={style.GallViewContent}>{article.content}</p>
</div>
<GaeChuButton parent={params.articleId} session={session} />
</div>
<Comment parent={params.articleId} />
</div>
);
}