Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
fix(sentry): catch getServerSideProps errors
Browse files Browse the repository at this point in the history
Catches and reports `getServerSideProps` errors to sentry explicitly.

This is an attempt to workaround a potential bug in Sentry: getsentry/sentry-javascript#7602
  • Loading branch information
masonmcelvain committed Mar 24, 2023
1 parent 960372b commit 5ca8a50
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 61 deletions.
11 changes: 11 additions & 0 deletions frontend/helpers/next-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
};
60 changes: 31 additions & 29 deletions frontend/templates/page/server.tsx
Original file line number Diff line number Diff line change
@@ -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<PageTemplateProps> = async (
context
) => {
if (!flags.STORE_HOME_PAGE_ENABLED) {
return {
notFound: true,
};
}
const withMiddleware = withSentry<PageTemplateProps>;

const layoutProps = await getLayoutServerSideProps({
storeCode: DEFAULT_STORE_CODE,
});
const page = await findPage({
path: '/Store',
});
export const getServerSideProps: GetServerSideProps<PageTemplateProps> =
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,
};
};
});
7 changes: 6 additions & 1 deletion frontend/templates/product-list/server.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -24,6 +28,7 @@ import { ProductListTemplateProps } from './hooks/useProductListTemplateProps';
import { ProductListView } from './ProductListView';

const withMiddleware = compose(
withSentry<ProductListTemplateProps>,
withLogging<ProductListTemplateProps>,
withCacheLong<ProductListTemplateProps>,
withNoindexDevDomains<ProductListTemplateProps>
Expand Down
7 changes: 6 additions & 1 deletion frontend/templates/product/server.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -11,6 +15,7 @@ import { GetServerSideProps } from 'next';
import { ProductTemplateProps } from './hooks/useProductTemplateProps';

const withMiddleware = compose(
withSentry<ProductTemplateProps>,
withLogging<ProductTemplateProps>,
withCacheLong<ProductTemplateProps>,
withNoindexDevDomains<ProductTemplateProps>
Expand Down
62 changes: 32 additions & 30 deletions frontend/templates/troubleshooting/server.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<TroubleshootingProps>;

const ifixitOrigin = ifixitOriginFromHost(context);
const client = new IFixitAPIClient({ origin: ifixitOrigin });
export const getServerSideProps: GetServerSideProps<TroubleshootingProps> =
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<TroubleshootingData>(
`Troubleshooting/${wikiname}`,
{
credentials: 'include',
if (!wikiname) {
return {
notFound: true,
};
}
);

const pageProps: TroubleshootingProps = {
wikiData,
layoutProps,
appProps: {
ifixitOrigin,
},
};
return {
props: pageProps,
};
};
const wikiData = await client.get<TroubleshootingData>(
`Troubleshooting/${wikiname}`,
{
credentials: 'include',
}
);

const pageProps: TroubleshootingProps = {
wikiData,
layoutProps,
appProps: {
ifixitOrigin,
},
};
return {
props: pageProps,
};
});

0 comments on commit 5ca8a50

Please sign in to comment.