Next.js Discord

Discord Forum

Server side error handling and showing specific things to users.

Unanswered
Masked Tityra posted this in #help-forum
Open in Discord
Masked TityraOP
Hey so i've read up on the error handling and gone through the posts here but I still have an issue understanding how to do this in a clean way.

I've made as many things as possible server side so for example I have a profile component which fetches user data. But that user data can get specific errors like an authorization error if they are not logged in or an api error if the user info service is not running stuff like that.

I have a different error helper that sets messages based on status code or logs it to specific locations stuff like that and that works fine after that I throw the errors.

Now I would like to actually show the error to the users. Since most of my stuff is server side I can't seem to use the error.tsx error boundary very well since while it shows the correct message during development if I make a build you get the error that error message from service side components could be unsafe so it doesn't show them I've seen that. So that only works for uncaught stuff and that is fine.

Now I could do try catches around every single component and render the error that way which does work however I was wondering if there was some kind of way to try catch all components at the same time and if there is an error that is being thrown I will show my Error component with the text I want server side.

I just want to make sure that when new components are created the error catching is not forgotten, could anyone explain to me if that is possible server side or that i'm stuck with having to try catch everything?

6 Replies

@Masked Tityra Hey so i've read up on the error handling and gone through the posts here but I still have an issue understanding how to do this in a clean way. I've made as many things as possible server side so for example I have a profile component which fetches user data. But that user data can get specific errors like an authorization error if they are not logged in or an api error if the user info service is not running stuff like that. I have a different error helper that sets messages based on status code or logs it to specific locations stuff like that and that works fine after that I throw the errors. Now I would like to actually show the error to the users. Since most of my stuff is server side I can't seem to use the error.tsx error boundary very well since while it shows the correct message during development if I make a build you get the error that error message from service side components could be unsafe so it doesn't show them I've seen that. So that only works for uncaught stuff and that is fine. Now I could do try catches around every single component and render the error that way which does work however I was wondering if there was some kind of way to try catch all components at the same time and if there is an error that is being thrown I will show my Error component with the text I want server side. I just want to make sure that when new components are created the error catching is not forgotten, could anyone explain to me if that is possible server side or that i'm stuck with having to try catch everything?
how about using normal react error boundaries?
though I've never used it myself, it should work
Masked TityraOP
I believe normal react error boundaries are client side are they not? I don't think they can catch server side errors that way if i'm not mistaken
sorry about that, I completely missed that.
I haven't done much of error handling but I did some using the vanilla Try catch
Masked TityraOP
Yeah it's not like the try catch doesn't work but a more elegant solution would be nice