Skip to content

Commit

Permalink
added more info about error types to SF middleware (#2753)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebaholesz authored Sep 13, 2023
2 parents 73256b7 + c3d7f73 commit f8093dc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
9 changes: 6 additions & 3 deletions storefront/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const middleware: NextMiddleware = async (request) => {
});

if (!pageTypeResponse.ok) {
return NextResponse.rewrite(new URL(ERROR_PAGE_ROUTE, request.url));
return NextResponse.rewrite(new URL(ERROR_PAGE_ROUTE, request.url), { status: 404 });
}

const pageTypeParsedResponse: { route: FriendlyPageTypesValue; redirectTo: string } =
Expand All @@ -60,7 +60,10 @@ export const middleware: NextMiddleware = async (request) => {
} catch (e) {
logException(e);

return NextResponse.rewrite(new URL(ERROR_PAGE_ROUTE, request.url));
return NextResponse.rewrite(new URL(ERROR_PAGE_ROUTE, request.url), {
status: 500,
statusText: 'Middleware runtime error',
});
}
};

Expand All @@ -81,7 +84,7 @@ const rewriteDynamicPages = (pageType: FriendlyPageTypesValue, rewriteUrl: strin
host,
);

return NextResponse.rewrite(newUrl);
return NextResponse.rewrite(newUrl, pageTypeKey ? undefined : { status: 404 });
};

const getHostFromRequest = (request: NextRequest): string => {
Expand Down
15 changes: 6 additions & 9 deletions storefront/pages/_error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ const ErrorPage: NextPage<ErrorPageProps> = ({ hasGetInitialPropsRun, err, statu
};

ErrorPage.getInitialProps = getServerSidePropsWrapper(({ redisClient, domainConfig, t }) => async (context: any) => {
const statusCode = context.res.statusCode || 500;
const errorInitialProps: any = await NextErrorComponent.getInitialProps({
res: context.res,
err: context.err,
err: context.err || statusCode === 500 ? context.res.statusText : null,
} as any);
const serverSideProps = await initServerSideProps({ context, redisClient, domainConfig, t });
// Workaround for https://github.com/vercel/next.js/issues/8592, mark when
Expand All @@ -48,22 +49,18 @@ ErrorPage.getInitialProps = getServerSidePropsWrapper(({ redisClient, domainConf
// Boundary. Read more about what types of exceptions are caught by Error
// Boundaries: https://reactjs.org/docs/error-boundaries.html

if (context.err) {
logException(context.err);
} else {
// If this point is reached, getInitialProps was called without any
// information about what the error might be. This is unexpected and may
// indicate a bug introduced in Next.js, so record it in Sentry
logException(new Error(`_error.js getInitialProps missing data at path: ${context.asPath}`));
if (statusCode !== 404) {
logException(context.err || new Error(`_error.js getInitialProps missing data at path: ${context.asPath}`));
}

// Flushing before returning is necessary if deploying to Vercel, see
// https://vercel.com/docs/platform/limits#streaming-responses
await flush(2000);
const props = 'props' in serverSideProps ? serverSideProps.props : {};

return {
...errorPageProps,
...('props' in serverSideProps ? serverSideProps.props : {}),
...{ ...props, statusCode },
};
});

Expand Down

0 comments on commit f8093dc

Please sign in to comment.