e03266008c
* form handler example: Update deps and fix build from dep update * Ran lint error fixers * Fixes errors that occur when commit occurs * Commit linter fixes
88 lines
2.2 KiB
JavaScript
88 lines
2.2 KiB
JavaScript
import App from 'next/app'
|
|
import sentry from '../utils/sentry'
|
|
|
|
const { Sentry, captureException } = sentry()
|
|
|
|
export default class MyApp extends App {
|
|
constructor () {
|
|
super(...arguments)
|
|
this.state = {
|
|
hasError: false,
|
|
errorEventId: undefined
|
|
}
|
|
}
|
|
|
|
static async getInitialProps ({ Component, ctx }) {
|
|
try {
|
|
let pageProps = {}
|
|
|
|
if (Component.getInitialProps) {
|
|
pageProps = await Component.getInitialProps(ctx)
|
|
}
|
|
|
|
return { pageProps }
|
|
} catch (error) {
|
|
// Capture errors that happen during a page's getInitialProps.
|
|
// This will work on both client and server sides.
|
|
const errorEventId = captureException(error, ctx)
|
|
return {
|
|
hasError: true,
|
|
errorEventId
|
|
}
|
|
}
|
|
}
|
|
|
|
static getDerivedStateFromProps (props, state) {
|
|
// If there was an error generated within getInitialProps, and we haven't
|
|
// yet seen an error, we add it to this.state here
|
|
return {
|
|
hasError: props.hasError || state.hasError || false,
|
|
errorEventId: props.errorEventId || state.errorEventId || undefined
|
|
}
|
|
}
|
|
|
|
static getDerivedStateFromError () {
|
|
// React Error Boundary here allows us to set state flagging the error (and
|
|
// later render a fallback UI).
|
|
return { hasError: true }
|
|
}
|
|
|
|
componentDidCatch (error, errorInfo) {
|
|
const errorEventId = captureException(error, { errorInfo })
|
|
|
|
// Store the event id at this point as we don't have access to it within
|
|
// `getDerivedStateFromError`.
|
|
this.setState({ errorEventId })
|
|
}
|
|
|
|
render () {
|
|
return this.state.hasError ? (
|
|
<section>
|
|
<h1>There was an error!</h1>
|
|
<p>
|
|
<a
|
|
href='#'
|
|
onClick={() =>
|
|
Sentry.showReportDialog({ eventId: this.state.errorEventId })
|
|
}
|
|
>
|
|
📣 Report this error
|
|
</a>
|
|
</p>
|
|
<p>
|
|
<a
|
|
href='#'
|
|
onClick={() => {
|
|
window.location.reload(true)
|
|
}}
|
|
>
|
|
Or, try reloading the page
|
|
</a>
|
|
</p>
|
|
</section>
|
|
) : (
|
|
// Render the normal Next.js page
|
|
super.render()
|
|
)
|
|
}
|
|
}
|