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

Explicitly capture getServerSideProps exceptions #1504

Closed
19 changes: 6 additions & 13 deletions frontend/helpers/next-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,10 @@ export const withNoindexDevDomains: GetServerSidePropsMiddleware = (next) => {
};

export const withSentry: GetServerSidePropsMiddleware = (next) => {
return (context) => {
return new Promise((resolve, reject) => {
next(context).then(resolve, (e) => {
Sentry.captureException(e);
console.log('Reporting exception to sentry');
console.log(e);
setTimeout(() => {
console.log('delayed for a bit, now carrying on');
reject(e);
}, 1000);
});
});
};
process.on('unhandledRejection', (reason) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we shouldn't be doing this inside the withSentry() call. Otherwise, if you use withSentry() more than once it will subscribe multiple times. Vercel is kinda special in that they only run one next.js page per process, but not every environment is like that.

I think we should do it at the module level (sentry.client.config and sentry.server.config)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yea good call. This implementation didn't report to Sentry, nor did I see the logs, but I will try it in the config file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Womp womp still no dice when registered in sentry.server.config

console.log('[Debug] Reporting exception to sentry', reason);
Sentry.captureException(reason);
console.log('[Debug] Reported to Sentry');
});
return next;
};
9 changes: 5 additions & 4 deletions frontend/pages/myPage.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { wrapGetServerSidePropsWithSentry } from '@sentry/nextjs';
import { withSentry } from '@helpers/next-helpers';
import { GetServerSideProps } from 'next';

const MyComponent = () => {
return <h1>Hello World!</h1>;
};

export const getServerSideProps: GetServerSideProps =
wrapGetServerSidePropsWithSentry(async (context) => {
export const getServerSideProps: GetServerSideProps = withSentry(
async (context) => {
if (context.query.myParam === 'two') {
// only throw conditionally so that this page actually builds
Promise.reject(new Error("We don't like page two"));
}

return { props: {} };
}, '/myPage');
}
);

export default MyComponent;