Next.js Discord

Discord Forum

Error handling best practices when using fetch in a server component using Next.js 14 App Router

Unanswered
African Slender-snouted Crocodil… posted this in #help-forum
Open in Discord
Original message was deleted.

5 Replies

Scottish Fold
I'm having a similar related error. I'm not sure what the best practice way to handle failing to fetch a resource that doesn't exist.

For example.

I have the a page at /blog/[uid] and if I try to serverside fetch a blog post that doesn't exist using my getBlogPost I currently just throw an error if the fetch fails (the fetch fails because it doesn't exist). I'm not sure what to do to trigger the not-found page to render if I go to /blog/non-existent-page
Giant panda
You check whether the ID exist and if it doesn't exist you call notFound()
Lapland Longspur
I am having similar issues too. I want to leverage error.tsx to show custom error content depending on the error resulting from a serverside fetch.


Because in prod as per docs "During production, the Error object forwarded to the client only includes a generic message and digest property."

what is the best way to go about this.

I also have a question https://nextjs-forum.com/post/1171235653875413136
@Giant panda You check whether the ID exist and if it doesn't exist you call `notFound()`
Lapland Longspur
yeah that is one way. What if you want to tell the customer "blog don't exist" using error.tsx that lives beside a page.tsx ? or if you want to show custom error message ?
Scottish Fold
notFound did the trick for me