Next.js Discord

Discord Forum

Utilize error.ts in prod

Unanswered
Lilac posted this in #help-forum
Open in Discord
LilacOP
In dev, simply throwing errors like
throw new Error("Message")
shows the user the error.ts fallback ui with the defined message.

In production, the message is replaced with
An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.


How do I implement the same dev behavior in prod?

8 Replies

LilacOP
I understand, but is there a way to achieve a similar behavior in prod?

For example if
res.status == 403
I would return the Error file with "You dont have permissions to access this page" message in prod. If throwing error wont work, what is the alternative?

Will I have to handle 403 erorr in every page and return something?
@Lilac I understand, but is there a way to achieve a similar behavior in prod? For example if res.status == 403 I would return the Error file with "You dont have permissions to access this page" message in prod. If throwing error wont work, what is the alternative? Will I have to handle 403 erorr in every page and return something?
There is no way to achieve this behavior using the error.js file afaik, this is because error.js is for unexpected errors, what you are describing is an expected error that you need to handle on your own.
@Lilac Would have been nice if it worked in prod too, in some way.
Too risky, can't leak sensitive data. Just handle the error yourself by creating an error component that renders on the page when the user doesn't have access with a button to return to a non-protected route or something similar depending on your use case
No problem