-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First-pass sentry integration (#822)
* add sentry * add sentry * linting * adding env to sentry * adding new envs vars to docker * PR branch name as release for sentry * let's try this * maybe this works * maybe this works * source maps * tidyup * tidyup * linting * handle sentry release in higher environments * removing release name form pr releases * removing debugging code * linting * document env varas in readme * formattign * fix typos
- Loading branch information
Anthony Sennett
authored
Feb 17, 2022
1 parent
2ca4c23
commit 4a19046
Showing
22 changed files
with
438 additions
and
12 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ yalc.lock | |
.idea | ||
.log | ||
.DS_Store | ||
.sentryclirc | ||
|
||
public/precache.*.*.js | ||
public/sw.js | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
web: yarn start:prod | ||
web: yarn start:prod |
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
12 changes: 12 additions & 0 deletions
12
components/errorTriggeringComponents/TriggerErrorWithUseObservable.tsx
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,12 @@ | ||
import React from 'react' | ||
import { of } from 'rxjs' | ||
import { flatMap } from 'rxjs/operators' | ||
|
||
import { useObservable } from '../../helpers/observableHook' | ||
|
||
const streamThatErrors$ = of(1).pipe(flatMap(() => fetch('https://fetch-unhandled-url'))) | ||
|
||
export function TriggerErrorWithUseObservable() { | ||
const value = useObservable(streamThatErrors$) | ||
return <>TriggerErrorWithUseObservable {JSON.stringify(value)}</> | ||
} |
15 changes: 15 additions & 0 deletions
15
components/errorTriggeringComponents/TriggerErrorWithUseObservableWithError.tsx
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,15 @@ | ||
import React from 'react' | ||
import { of } from 'rxjs' | ||
import { flatMap } from 'rxjs/operators' | ||
|
||
import { useObservableWithError } from '../../helpers/observableHook' | ||
const streamThatErrors$ = of(1).pipe(flatMap(() => fetch('https://fetch-handled-url'))) | ||
export function TriggerErrorWithUseObservableWithError() { | ||
const { value, error } = useObservableWithError(streamThatErrors$) | ||
return ( | ||
<> | ||
TriggerErrorWithUseObservableWithError. value: {JSON.stringify(value)} <br /> error:{' '} | ||
{JSON.stringify(error)} | ||
</> | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import NextErrorComponent from 'next/error' | ||
|
||
import * as Sentry from '@sentry/nextjs' | ||
|
||
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, asPath }) => { | ||
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) | ||
|
||
return errorInitialProps | ||
} | ||
|
||
// If this point is reached, getInitialProps was called without any | ||
// information about what the error might be. This is unexpected and may | ||
// indicate a bug introduced in Next.js, so record it in Sentry | ||
Sentry.captureException(new Error(`_error.js getInitialProps missing data at path: ${asPath}`)) | ||
await Sentry.flush(2000) | ||
|
||
return errorInitialProps | ||
} | ||
|
||
export default MyError |
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,7 @@ | ||
import { withSentry } from '@sentry/nextjs' | ||
|
||
const handler = async () => { | ||
throw new Error('API throw error test') | ||
} | ||
|
||
export default withSentry(handler) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import { withSentry } from '@sentry/nextjs' | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
|
||
export default async function (_req: NextApiRequest, res: NextApiResponse) { | ||
const handler = async function (_req: NextApiRequest, res: NextApiResponse) { | ||
const response = { status: 200, message: 'Everything is okay!' } | ||
res.status(200).json(response) | ||
} | ||
|
||
export default withSentry(handler) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { MarketingLayout } from 'components/Layouts' | ||
import { serverSideTranslations } from 'next-i18next/serverSideTranslations' | ||
import React, { useState } from 'react' | ||
|
||
import { TriggerErrorWithUseObservable } from '../../components/errorTriggeringComponents/TriggerErrorWithUseObservable' | ||
import { TriggerErrorWithUseObservableWithError } from '../../components/errorTriggeringComponents/TriggerErrorWithUseObservableWithError' | ||
|
||
export const getStaticProps = async ({ locale }: { locale: string }) => ({ | ||
props: { | ||
...(await serverSideTranslations(locale, ['common'])), | ||
}, | ||
}) | ||
|
||
export default function ServerError() { | ||
const [ | ||
showComponentThatErrorsWithUnhandledError, | ||
setShowComponentThatErrorsWithUnhandledError, | ||
] = useState(false) | ||
|
||
const [ | ||
showComponentThatErrorsWithHandledError, | ||
setShowComponentThatErrorsWithHandledError, | ||
] = useState(false) | ||
return ( | ||
<ul> | ||
<li> | ||
<button | ||
type="button" | ||
onClick={() => { | ||
throw new Error('Sentry Frontend Error') | ||
}} | ||
> | ||
Trigger error on client in this component | ||
</button> | ||
</li> | ||
<li> | ||
<button | ||
type="button" | ||
onClick={() => { | ||
setShowComponentThatErrorsWithUnhandledError( | ||
(showComponentThatTriggersUnhandledError) => !showComponentThatTriggersUnhandledError, | ||
) | ||
}} | ||
> | ||
Trigger handled error in observable with useObservable | ||
</button> | ||
{showComponentThatErrorsWithUnhandledError && <TriggerErrorWithUseObservable />} | ||
</li> | ||
<li> | ||
<button | ||
type="button" | ||
onClick={() => { | ||
setShowComponentThatErrorsWithHandledError( | ||
(showComponentThatErrors) => !showComponentThatErrors, | ||
) | ||
}} | ||
> | ||
Trigger handled error in observable with useObservableWithError | ||
</button> | ||
{showComponentThatErrorsWithHandledError && <TriggerErrorWithUseObservableWithError />} | ||
</li> | ||
<li> | ||
<a href="/errors/server-error">trigger error on page on server</a> | ||
</li> | ||
<li> | ||
<a href="/api/deliberateError">trigger error on API endpoint</a> | ||
</li> | ||
</ul> | ||
) | ||
} | ||
|
||
ServerError.layout = MarketingLayout | ||
ServerError.theme = 'Landing' |
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,18 @@ | ||
import { MarketingLayout } from 'components/Layouts' | ||
import { serverSideTranslations } from 'next-i18next/serverSideTranslations' | ||
|
||
export const getStaticProps = async ({ locale }: { locale: string }) => ({ | ||
props: { | ||
...(await serverSideTranslations(locale, ['common'])), | ||
}, | ||
}) | ||
|
||
export default function ServerError() { | ||
throw new Error('test error from page') | ||
} | ||
|
||
ServerError.layout = MarketingLayout | ||
ServerError.layoutProps = { | ||
variant: 'termsContainer', | ||
} | ||
ServerError.theme = 'Landing' |
Oops, something went wrong.