How can I leverage error.tsx to display custom error messages depending on the kind of error happened in the server?
During production the actual error message is stripped out and replaced with a generic error messaging.
I find this makes it difficult to use error.tsx to show custom error content
For example, code blocks below don't work in production because next says:
During production, the
Error
object forwarded to the client only includes a genericmessage
anddigest
property. - This means below example won't work in productionDuring development, the
Error
object forwarded to the client will be serialized and include themessage
of the original error for easier debugging. - this means below example works only during development
- page.tsx - app/dashboard/page.tsx
export default async function Page() {
const { data } = await getUsers();
return (
//List of users
);
}
- error.tsx - app/dashboard/error.tsx
'use client';
export default function Error({ error, reset }: { error: Error & { digest?: string }; reset: () => void }) {
return (
<div>
{error.message === 'UNAUTHORIZED' ? (
<>
<h1>UNAUTHORIZED</h1>
<button onClick={reset}>Try again !!!</button>
</>
) : error.message === 'INTERNAL_SERVER_ERROR' ? (
<>
<h1>UNAUTHORIZED</h1>
<button onClick={reset}>Try again !!!</button>
</>
) : (
<>
<h1>Ooops</h1>
<h1>Something went wrong!!!</h1>
</>
)}
</div>
);
}
If server actiongetUsers()
results in errors like UNAUTHORIZED (401)
, or INTERNAL_SERVER_ERROR (500) I expect error.tsx to render the right content. In development it works fine, but in production it would always render the last block.
What am I missing?
Am i wrong for thinking this is the way it should be handled?
How can i go about showing the right error to the user in the UI