1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

update with-sentry example (#4859)

Changes:

* moved the configuration from HOC to `_app.js`
* fixed the example, as `componentDidCatch` catches errors during rendering phase, but not within event handlers.
This commit is contained in:
Tomek 2018-08-04 19:51:35 +02:00 committed by Tim Neutkens
parent bd3f65b37f
commit b1459bfd60
3 changed files with 28 additions and 44 deletions

View file

@ -1,35 +0,0 @@
import React from 'react'
import Raven from 'raven-js'
const SENTRY_DSN = ''
function withSentry (Child) {
return class WrappedComponent extends React.Component {
static getInitialProps (context) {
if (Child.getInitialProps) {
return Child.getInitialProps(context)
}
return {}
}
constructor (props) {
super(props)
this.state = {
error: null
}
Raven.config(
SENTRY_DSN
).install()
}
componentDidCatch (error, errorInfo) {
this.setState({ error })
Raven.captureException(error, { extra: errorInfo })
}
render () {
return <Child {...this.props} error={this.state.error} />
}
}
}
export default withSentry

View file

@ -0,0 +1,18 @@
import App from 'next/app'
import Raven from 'raven-js'
const SENTRY_PUBLIC_DSN = ''
export default class MyApp extends App {
constructor (...args) {
super(...args)
Raven.config(SENTRY_PUBLIC_DSN).install()
}
componentDidCatch (error, errorInfo) {
Raven.captureException(error, { extra: errorInfo })
// This is needed to render errors correctly in development / production
super.componentDidCatch(error, errorInfo)
}
}

View file

@ -1,25 +1,26 @@
import React from 'react'
import withSentry from '../components/withSentry'
class Index extends React.Component {
static getInitialProps (context) {
const { isServer } = context
return { isServer }
state = {
raiseError: false
}
onClickHandler () {
throw new Error('woops')
componentDidUpdate () {
if (this.state.raiseError) {
throw new Error('Houston, we have a problem')
}
}
raiseError = () => this.setState({ raiseError: true })
render () {
return (
<div>
<h2>Index page</h2>
<button onClick={this.onClickHandler.bind(this)}>Click to raise error</button>
<button onClick={this.raiseError}>Click to raise the error</button>
</div>
)
}
}
export default withSentry(Index)
export default Index