Given a Server Component (or Server Action) that fetches data, how can I access the full error message for server-side logging purposes?
Example:
import os from 'os';
import { PHASE_PRODUCTION_BUILD } from 'next/constants';
export const dynamic = 'force-dynamic'
async function getDataFromServer(): Promise<string> {
if (process.env.NEXT_PHASE !== PHASE_PRODUCTION_BUILD) {
throw new Error('Error from server');
}
return Promise.resolve(`OS Type: ${os.type()}`);
}
export default async function Home() {
const serverData = await getDataFromServer();
return (
<p>From server: {serverData}</p>
)
}
Running this in a "production" version (npm run build, npm run start) results in
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.
I do not want to expose the actual error message to clients, but I want to log it to a service server-side. Error boundaries are client-side, so they get the sanitized message.
The only way I've found is to wrap every server method in try..catch. I can create a higher-order function to reduce the boilerplate, but is there a way to create a single server-side error handler (without delegating from every catch block)?