Next.js Discord

Discord Forum

Handling Server Errors

Unanswered
Lapland Longspur posted this in #help-forum
Open in Discord
Lapland LongspurOP
Is it possible to leverage error.tsx to display custom error message depending on error occured from a fetch call in a React Server Component.


This is the sort of pattern I am thinking of. But according to next doc "During production, the Error object forwarded to the client only includes a generic message and digest property." This means the way I am using error.tsx will not work as I cannot read error.message in prod


Does this make it impossible to use the pattern I am thinking of ?

4 Replies

Lapland LongspurOP
Would love some ideas on this ?
Komondor
Since you're on the client I think you'd need to detect that the call returned an error and then redirect to the error page.
@Komondor Since you're on the client I think you'd need to detect that the call returned an error and then redirect to the error page.
Lapland LongspurOP
Thanks for your message.
I think redirect is out of the question because the error.tsx client component is an error boundary around the page which I want to leverage. There is no need to redirect.
I think the way I’m thinking of it is incorrect. Doc does mention that the error.tsx is a “catch all”. Maybe the fact that for security reasons error message stripped away and it’s made generic probably suggests next want us to show generic error page 🤷🏾.


Would’ve been nice if we could at least pass our own custom error messages to it.

It’s weird that not many ppl wondering about this.


Maybe it’s a misconception on this on my end @Siberian
Shy Albatross
error.ts is adding an error boundary for you, which you can do manually wherever you need it.
the bit about passing a generic error is true for a scenario where you throw in your server action.
if instead your server action returns a { message: "oops" } then you can check that on the client, and decide to throw from your client component that called the action. That then should be caugh with the details as you expect