Next.js Discord

Discord Forum

How to show errors in server component?

Unanswered
Chartreux posted this in #help-forum
Open in Discord
ChartreuxOP
For example, I have a page that is a server component that performs data fetching. If something goes wrong, I want to show an error. Usually, in React, I just add a modal or a toaster, but since it's a server component, how can I handle that error?

2 Replies

Asian black bear
You likely fetch data using a promise and can use try/catch or other more sophisticated methods of handling promise rejections and errors. In that case you can render an error message.
Highly depends on whether you use try/catch, a convenient wrapper around data fetching or things like neverthrow or Effect.