Skip to main content

Function: handleError()

handleError(error, ctx, rootRoute): Promise<{ notFound?: undefined; redirect: { destination: string; permanent: boolean; }; } | { notFound: true; redirect?: undefined; }>

Defined in: packages/next/src/data/server/handleError.ts:62

The handleError function is responsible for handling errors that occur during data fetching in getServerSideProps or getStaticProps.

It also handles redirects if redirectStrategy is set to 404 in headless.config.js

If error is of type NotFoundError it will redirect to the 404 page. Otherwise it will return a server error (500) page

Usage​

export async function getServerSideProps(context) {
try {
const usePostsHook = await fetchHookData(usePosts.fetcher(),context);
return addHookData([usePostsHook], {});
} catch (e) {
return handleError(e, context);
}
}

Parameters​

error​

Error

The error object

ctx​

The Next.js context

HeadlessGetServerSidePropsContext | HeadlessGetStaticPropsPropsContext

rootRoute​

string = ''

The root route (deprecated/unnecessary). This needs to be revisited

Returns​

Promise<{ notFound?: undefined; redirect: { destination: string; permanent: boolean; }; } | { notFound: true; redirect?: undefined; }>