This repository has been archived by the owner on Dec 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Explicitly capture getServerSideProps
exceptions
#1504
Closed
masonmcelvain
wants to merge
9
commits into
main
from
explicitly-capture-serverside-props-exceptions
Closed
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5b11e77
chore(deps): upgrade sentry to `7.45.0`
masonmcelvain 960372b
fix(sentry): update _error page
masonmcelvain 5ca8a50
fix(sentry): catch `getServerSideProps` errors
masonmcelvain 572ad9a
debug: try `wrapGetServerSidePropsWithSentry`
masonmcelvain 6053587
WIP
danielbeardsley 601bb19
Add logging
danielbeardsley 8b6529b
style: apply prettier formatting diff
github-actions[bot] 3e10ee7
debug(sentry): listen to `unhandledRejection`
masonmcelvain c505731
debug(sentry): prevent duplicate event listeners
masonmcelvain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* NOTE: This requires `@sentry/nextjs` version 7.3.0 or higher. | ||
* | ||
* This page is loaded by Nextjs: | ||
* - on the server, when data-fetching methods throw or reject | ||
* - on the client, when `getInitialProps` throws or rejects | ||
* - on the client, when a React lifecycle method throws or rejects, and it's | ||
* caught by the built-in Nextjs error boundary | ||
* | ||
* See: | ||
* - https://nextjs.org/docs/basic-features/data-fetching/overview | ||
* - https://nextjs.org/docs/api-reference/data-fetching/get-initial-props | ||
* - https://reactjs.org/docs/error-boundaries.html | ||
*/ | ||
|
||
import * as Sentry from '@sentry/nextjs'; | ||
import type { NextPage } from 'next'; | ||
import type { ErrorProps } from 'next/error'; | ||
import NextErrorComponent from 'next/error'; | ||
|
||
const CustomErrorComponent: NextPage<ErrorProps> = (props) => { | ||
// If you're using a Nextjs version prior to 12.2.1, uncomment this to | ||
// compensate for https://github.com/vercel/next.js/issues/8592 | ||
// Sentry.captureUnderscoreErrorException(props); | ||
|
||
return <NextErrorComponent statusCode={props.statusCode} />; | ||
}; | ||
|
||
CustomErrorComponent.getInitialProps = async (contextData) => { | ||
// In case this is running in a serverless function, await this in order to give Sentry | ||
// time to send the error before the lambda exits | ||
await Sentry.captureUnderscoreErrorException(contextData); | ||
|
||
// This will contain the status code of the response | ||
return NextErrorComponent.getInitialProps(contextData); | ||
}; | ||
|
||
export default CustomErrorComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { withSentry } from '@helpers/next-helpers'; | ||
import { GetServerSideProps } from 'next'; | ||
|
||
const MyComponent = () => { | ||
return <h1>Hello World!</h1>; | ||
}; | ||
|
||
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: {} }; | ||
} | ||
); | ||
|
||
export default MyComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 usewithSentry()
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
andsentry.server.config
)There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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