Skip to content

Commit

Permalink
update with-sentry example (#4859)
Browse files Browse the repository at this point in the history
Changes:

* moved the configuration from HOC to `_app.js`
* fixed the example, as `componentDidCatch` catches errors during rendering phase, but not within event handlers.
  • Loading branch information
Tomekmularczyk authored and timneutkens committed Aug 4, 2018
1 parent bd3f65b commit b1459bf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 44 deletions.
35 changes: 0 additions & 35 deletions examples/with-sentry/components/withSentry.js

This file was deleted.

18 changes: 18 additions & 0 deletions examples/with-sentry/pages/_app.js
Original file line number Diff line number Diff line change
@@ -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)
}
}
19 changes: 10 additions & 9 deletions examples/with-sentry/pages/index.js
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b1459bf

Please sign in to comment.