[Handling Errors] - In production mode, explicit error messages for server components #62681
-
SummaryContext : NextJS 14.0.3, Typescript, App router In this application, I wanted to handle errors almost identically, wether they are client or server side. 'use client';
import React from 'react';
import type { Metadata } from 'next';
import { ErrorContent } from '@/app/components/atomic';
export const metadata: Metadata = {
title: 'My Title',
description: 'Something went wrong',
};
export default function Error({
error,
reset,
}: {
error: Error & { digest?: string };
reset: () => void;
}) {
return <ErrorContent {...{ error, reset }} />;
} As I could'nt extend the Error class to create a custom HttpError class (with status property for instance) and use the extended class with a error.tsx file, I found this kind of work around, aggregating the status in the error message : import React from 'react';
import { cookies } from 'next/headers';
import { ssrRequest } from '@/utils';
import { Schema, shipDefaultValuesSchema } from '@/schemas';
import type { Metadata } from 'next';
import { notFound } from 'next/navigation';
export const generateMetadata = async ({
searchParams,
}: {
searchParams: { ship_id: string };
}): Promise<Metadata> => {
try {
const id = searchParams.id;
const data= await ssrRequest(
`${process.env.API_URL}/sata/${id}`,
schema,
'GET',
cookies()
);
return data;
};
} catch (error) {
if (error instanceof Error) {
if (error.message.includes('404')) return notFound();
throw error;
}
throw new Error('Metadata generation failed');
}
}; NextJS censors error message outputs in production as follow : As I control all kind of emitted errors and some contain essential feedbacks for the user, I wonder if there is a way to perform a clean error handling for server components. Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 20 replies
-
Losing a bit of hope with the lack of official information or responses on this matter, basically all threads I follow on error tracking are dead ends... |
Beta Was this translation helpful? Give feedback.
-
You can probably use sentry to enhance access to error information by listening for errors in server components and server action |
Beta Was this translation helpful? Give feedback.
-
Can you walk me through a product UI example and the intended output? It sounds like you want to catch an error on the server, and then display that message to the client. Is this for that case, or is it about unhandled exceptions? To confirm, this is not about just viewing the server-side error logs? |
Beta Was this translation helpful? Give feedback.
-
Thank you all for the feedback in this discussion. I've updated the error handling docs to hopefully make this more clear. https://rc.nextjs.org/docs/app/building-your-application/routing/error-handling |
Beta Was this translation helpful? Give feedback.
-
Based on @RNm-dove proposal (many thanks), I worked it in a similar way as follow : import React from 'react';
import { ErrorContent } from '@/app/components/atomic';
import { ErrorPageWrapper } from './page-wrapper';
import { notFound } from 'next/navigation';
export const serverComponentErrorBoundary = async (
fn: () => Promise<JSX.Element>
) => {
try {
return await fn();
} catch (error) {
if (error instanceof Error) {
if (error.message.includes('404')) return notFound();
return <ErrorContent error={error} />;
}
throw error;
}
}; I use it as follow : export default async function Page({
searchParams,
}: {
searchParams: SearchParams;
}) {
// Preliminary operations, independent from data fetching
return serverComponentErrorBoundary(async () => {
const data1= await getData1();
const data2= await getData2();
const data3= await getData3();
return (
<PageComponent
{...{
seachParams,
data1,
data2,
data3,
}}
/>
);
});
} Each case (weather it is a whole page, a nested page or a component) is quite particular, so the UI chunk fallback has to be adapted for each case. Huge thanks to you all. |
Beta Was this translation helpful? Give feedback.
Thank you all for the feedback in this discussion. I've updated the error handling docs to hopefully make this more clear.
https://rc.nextjs.org/docs/app/building-your-application/routing/error-handling