From 5ca8a50e5ec077f448e346be3606a007e88e21b3 Mon Sep 17 00:00:00 2001 From: Mason McElvain <52104630+masonmcelvain@users.noreply.github.com> Date: Fri, 24 Mar 2023 16:24:52 -0700 Subject: [PATCH] fix(sentry): catch `getServerSideProps` errors Catches and reports `getServerSideProps` errors to sentry explicitly. This is an attempt to workaround a potential bug in Sentry: https://github.com/getsentry/sentry-javascript/issues/7602 --- frontend/helpers/next-helpers.ts | 11 ++++ frontend/templates/page/server.tsx | 60 +++++++++--------- frontend/templates/product-list/server.tsx | 7 ++- frontend/templates/product/server.tsx | 7 ++- frontend/templates/troubleshooting/server.tsx | 62 ++++++++++--------- 5 files changed, 86 insertions(+), 61 deletions(-) diff --git a/frontend/helpers/next-helpers.ts b/frontend/helpers/next-helpers.ts index 0e8d88d5..c33e1584 100644 --- a/frontend/helpers/next-helpers.ts +++ b/frontend/helpers/next-helpers.ts @@ -50,3 +50,14 @@ export const withNoindexDevDomains: GetServerSidePropsMiddleware = (next) => { return result; }; }; + +export const withSentry: GetServerSidePropsMiddleware = (next) => { + return (context) => { + try { + return next(context); + } catch (e) { + Sentry.captureException(e); + throw e; + } + }; +}; diff --git a/frontend/templates/page/server.tsx b/frontend/templates/page/server.tsx index 044ffc0d..3796b68e 100644 --- a/frontend/templates/page/server.tsx +++ b/frontend/templates/page/server.tsx @@ -1,41 +1,43 @@ import { DEFAULT_STORE_CODE } from '@config/env'; import { flags } from '@config/flags'; +import { withSentry } from '@helpers/next-helpers'; import { ifixitOriginFromHost } from '@helpers/path-helpers'; import { getLayoutServerSideProps } from '@layouts/default/server'; import { findPage } from '@models/page/server'; import { GetServerSideProps } from 'next'; import { PageTemplateProps } from './hooks/usePageTemplateProps'; -export const getServerSideProps: GetServerSideProps = async ( - context -) => { - if (!flags.STORE_HOME_PAGE_ENABLED) { - return { - notFound: true, - }; - } +const withMiddleware = withSentry; - const layoutProps = await getLayoutServerSideProps({ - storeCode: DEFAULT_STORE_CODE, - }); - const page = await findPage({ - path: '/Store', - }); +export const getServerSideProps: GetServerSideProps = + withMiddleware(async (context) => { + if (!flags.STORE_HOME_PAGE_ENABLED) { + return { + notFound: true, + }; + } + + const layoutProps = await getLayoutServerSideProps({ + storeCode: DEFAULT_STORE_CODE, + }); + const page = await findPage({ + path: '/Store', + }); - if (page == null) { + if (page == null) { + return { + notFound: true, + }; + } + + const pageProps: PageTemplateProps = { + layoutProps, + appProps: { + ifixitOrigin: ifixitOriginFromHost(context), + }, + page, + }; return { - notFound: true, + props: pageProps, }; - } - - const pageProps: PageTemplateProps = { - layoutProps, - appProps: { - ifixitOrigin: ifixitOriginFromHost(context), - }, - page, - }; - return { - props: pageProps, - }; -}; + }); diff --git a/frontend/templates/product-list/server.tsx b/frontend/templates/product-list/server.tsx index 7b0a6f4d..e5a9aee5 100644 --- a/frontend/templates/product-list/server.tsx +++ b/frontend/templates/product-list/server.tsx @@ -1,7 +1,11 @@ import { AppProviders, AppProvidersProps } from '@components/common'; import { ALGOLIA_PRODUCT_INDEX_NAME, DEFAULT_STORE_CODE } from '@config/env'; import { withCacheLong } from '@helpers/cache-control-helpers'; -import { withLogging, withNoindexDevDomains } from '@helpers/next-helpers'; +import { + withLogging, + withNoindexDevDomains, + withSentry, +} from '@helpers/next-helpers'; import { ifixitOriginFromHost } from '@helpers/path-helpers'; import { destylizeDeviceItemType, @@ -24,6 +28,7 @@ import { ProductListTemplateProps } from './hooks/useProductListTemplateProps'; import { ProductListView } from './ProductListView'; const withMiddleware = compose( + withSentry, withLogging, withCacheLong, withNoindexDevDomains diff --git a/frontend/templates/product/server.tsx b/frontend/templates/product/server.tsx index 03c2c0c6..605fae87 100644 --- a/frontend/templates/product/server.tsx +++ b/frontend/templates/product/server.tsx @@ -1,6 +1,10 @@ import { DEFAULT_STORE_CODE } from '@config/env'; import { withCacheLong } from '@helpers/cache-control-helpers'; -import { withLogging, withNoindexDevDomains } from '@helpers/next-helpers'; +import { + withLogging, + withNoindexDevDomains, + withSentry, +} from '@helpers/next-helpers'; import { ifixitOriginFromHost } from '@helpers/path-helpers'; import { invariant } from '@ifixit/helpers'; import { urlFromContext } from '@ifixit/helpers/nextjs'; @@ -11,6 +15,7 @@ import { GetServerSideProps } from 'next'; import { ProductTemplateProps } from './hooks/useProductTemplateProps'; const withMiddleware = compose( + withSentry, withLogging, withCacheLong, withNoindexDevDomains diff --git a/frontend/templates/troubleshooting/server.tsx b/frontend/templates/troubleshooting/server.tsx index 49a7317d..2f8f3277 100644 --- a/frontend/templates/troubleshooting/server.tsx +++ b/frontend/templates/troubleshooting/server.tsx @@ -1,4 +1,5 @@ import { DEFAULT_STORE_CODE } from '@config/env'; +import { withSentry } from '@helpers/next-helpers'; import { ifixitOriginFromHost } from '@helpers/path-helpers'; import { IFixitAPIClient } from '@ifixit/ifixit-api-client'; import { getLayoutServerSideProps } from '@layouts/default/server'; @@ -8,39 +9,40 @@ import { TroubleshootingData, } from './hooks/useTroubleshootingProps'; -export const getServerSideProps: GetServerSideProps< - TroubleshootingProps -> = async (context) => { - const layoutProps = await getLayoutServerSideProps({ - storeCode: DEFAULT_STORE_CODE, - }); +const withMiddleware = withSentry; - const ifixitOrigin = ifixitOriginFromHost(context); - const client = new IFixitAPIClient({ origin: ifixitOrigin }); +export const getServerSideProps: GetServerSideProps = + withMiddleware(async (context) => { + const layoutProps = await getLayoutServerSideProps({ + storeCode: DEFAULT_STORE_CODE, + }); - const wikiname = context.params?.wikiname; + const ifixitOrigin = ifixitOriginFromHost(context); + const client = new IFixitAPIClient({ origin: ifixitOrigin }); - if (!wikiname) { - return { - notFound: true, - }; - } + const wikiname = context.params?.wikiname; - const wikiData = await client.get( - `Troubleshooting/${wikiname}`, - { - credentials: 'include', + if (!wikiname) { + return { + notFound: true, + }; } - ); - const pageProps: TroubleshootingProps = { - wikiData, - layoutProps, - appProps: { - ifixitOrigin, - }, - }; - return { - props: pageProps, - }; -}; + const wikiData = await client.get( + `Troubleshooting/${wikiname}`, + { + credentials: 'include', + } + ); + + const pageProps: TroubleshootingProps = { + wikiData, + layoutProps, + appProps: { + ifixitOrigin, + }, + }; + return { + props: pageProps, + }; + });