rsnext/examples/with-sentry/pages/_app.js

89 lines
2.2 KiB
JavaScript
Raw Normal View History

import App from 'next/app'
2019-02-20 14:49:16 +01:00
import sentry from '../utils/sentry'
const { Sentry, captureException } = sentry()
export default class MyApp extends App {
2019-02-20 14:49:16 +01:00
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) {
2019-02-20 14:49:16 +01:00
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 })
}
2019-02-20 14:49:16 +01:00
>
📣 Report this error
</a>
</p>
<p>
<a
href='#'
onClick={() => {
window.location.reload(true)
}}
2019-02-20 14:49:16 +01:00
>
Or, try reloading the page
</a>
</p>
</section>
) : (
// Render the normal Next.js page
super.render()
)
}
}