-
Notifications
You must be signed in to change notification settings - Fork 29.9k
feat(examples): Use helper function in with-sentry example _error.js
#37866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kodiakhq
merged 4 commits into
vercel:canary
from
lobsterkatie:kmclb-use-helper-function-in-with-sentry-_error
Jul 15, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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,56 +1,30 @@ | ||
| import NextErrorComponent from 'next/error' | ||
| /** | ||
| * 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 NextErrorComponent from 'next/error' | ||
|
|
||
| const MyError = ({ statusCode, hasGetInitialPropsRun, err }) => { | ||
| if (!hasGetInitialPropsRun && err) { | ||
| // getInitialProps is not called in case of | ||
| // https://github.com/vercel/next.js/issues/8592. As a workaround, we pass | ||
| // err via _app.js so it can be captured | ||
| Sentry.captureException(err) | ||
| // Flushing is not required in this case as it only happens on the client | ||
| } | ||
|
|
||
| return <NextErrorComponent statusCode={statusCode} /> | ||
| } | ||
|
|
||
| MyError.getInitialProps = async ({ res, err }) => { | ||
| const errorInitialProps = await NextErrorComponent.getInitialProps({ | ||
| res, | ||
| err, | ||
| }) | ||
|
|
||
| // Workaround for https://github.com/vercel/next.js/issues/8592, mark when | ||
| // getInitialProps has run | ||
| errorInitialProps.hasGetInitialPropsRun = true | ||
|
|
||
| // Running on the server, the response object (`res`) is available. | ||
| // | ||
| // Next.js will pass an err on the server if a page's data fetching methods | ||
| // threw or returned a Promise that rejected | ||
| // | ||
| // Running on the client (browser), Next.js will provide an err if: | ||
| // | ||
| // - a page's `getInitialProps` threw or returned a Promise that rejected | ||
| // - an exception was thrown somewhere in the React lifecycle (render, | ||
| // componentDidMount, etc) that was caught by Next.js's React Error | ||
| // Boundary. Read more about what types of exceptions are caught by Error | ||
| // Boundaries: https://reactjs.org/docs/error-boundaries.html | ||
|
|
||
| if (err) { | ||
| Sentry.captureException(err) | ||
|
|
||
| // Flushing before returning is necessary if deploying to Vercel, see | ||
| // https://vercel.com/docs/platform/limits#streaming-responses | ||
| await Sentry.flush(2000) | ||
| const CustomErrorComponent = (props) => ( | ||
| <NextErrorComponent statusCode={props.statusCode} /> | ||
| ) | ||
|
|
||
| return errorInitialProps | ||
| } | ||
| 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) | ||
|
|
||
| // If this point is reached, getInitialProps was called without any | ||
| // information about what the error might be. This can be caused by | ||
| // a falsy value being thrown e.g. throw undefined | ||
| return errorInitialProps | ||
| // This will contain the status code of the response | ||
| return NextErrorComponent.getInitialProps(contextData) | ||
| } | ||
|
|
||
| export default MyError | ||
| export default CustomErrorComponent | ||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.