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

Added Sentry.io example (#3215)

* Added Sentry example

* Code style fixes

* Fixes docs + removed demo DSN + send error to comp
This commit is contained in:
Leandro Ardissone 2017-11-04 11:06:16 -03:00 committed by Tim Neutkens
parent b41d177609
commit a35e747e24
4 changed files with 109 additions and 0 deletions

View file

@ -0,0 +1,33 @@
[![Deploy to now](https://deploy.now.sh/static/button.svg)](https://deploy.now.sh/?repo=https://github.com/zeit/next.js/tree/master/examples/with-sentry)
# Sentry example
## How to use
Download the example [or clone the repo](https://github.com/zeit/next.js):
Install it and run:
**npm**
```bash
npm install
npm run dev
```
**yarn**
```bash
npm install
npm run dev
```
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
```bash
now
```
## About example
This example show you how to add Sentry to catch errors in next.js
You will need a Sentry DSN for your project. You can get it from the Settings of your Project, in **Client Keys (DSN)**, and copy the string labeled **DSN (Public)**.

View file

@ -0,0 +1,35 @@
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,16 @@
{
"name": "with-sentry",
"version": "1.0.0",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"raven-js": "^3.19.1",
"react": "^16.0.0",
"react-dom": "^16.0.0"
},
"license": "ISC"
}

View file

@ -0,0 +1,25 @@
import React from 'react'
import withSentry from '../components/withSentry'
class Index extends React.Component {
static getInitialProps (context) {
const { isServer } = context
return { isServer }
}
onClickHandler () {
throw new Error('woops')
}
render () {
return (
<div>
<h2>Index page</h2>
<button onClick={this.onClickHandler.bind(this)}>Click to raise error</button>
</div>
)
}
}
export default withSentry(Index)