Skip to content

Commit

Permalink
feat: use new sentry sdk, close #20, #30
Browse files Browse the repository at this point in the history
  • Loading branch information
aldarund authored and DiederikvandenB committed Oct 19, 2018
1 parent 1751c65 commit da63340
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 74 deletions.
13 changes: 1 addition & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ The module enables error logging through [Sentry](http://sentry.io).

sentry: {
public_key: '',
private_key: '',
project_id: '',
config: {
// Additional config
Expand All @@ -42,7 +41,7 @@ Enter your DSN in the NuxtJS config file. Additional config settings can be foun

### Usage in Vue component

In a Vue component, `Raven` is available as `this.$raven`, so we can call functions like
In a Vue component, `Sentry` is available as `this.$sentry`, so we can call functions like

```
this.$raven.setUserContext({user})
Expand All @@ -59,23 +58,13 @@ Normally setting required DSN information would be enough.
- Type: `String`
- Default: `process.env.SENTRY_DSN`

### public_dsn
- Type: `String`
- Default: `process.env.SENTRY_PUBLIC_DSN`

If value omitted it will be generated using `dsn` value, by removing private key part.

### public_key
- Type: `String`
- Default: `process.env.SENTRY_PUBLIC_KEY`

Will be ignored if `dsn` provided.

### private_key
- Type: `String`
- Default: `process.env.SENTRY_PRIVATE_KEY`

Will be ignored if `dsn` provided.

### host
- Type: `String`
Expand Down
40 changes: 18 additions & 22 deletions lib/sentry.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Raven = require('raven')
const Sentry = require('@sentry/node')
const path = require('path')

const logger = require('consola').withScope('nuxt:sentry')
Expand All @@ -9,9 +9,7 @@ module.exports = function sentry (moduleOptions) {
disabled: process.env.SENTRY_DISABLED || false,
disableClientSide: process.env.SENTRY_DISABLE_CLIENT_SIDE === 'true' || false,
dsn: process.env.SENTRY_DSN || null,
public_dsn: process.env.SENTRY_PUBLIC_DSN || null,
public_key: process.env.SENTRY_PUBLIC_KEY || null,
private_key: process.env.SENTRY_PRIVATE_KEY || null,
host: process.env.SENTRY_HOST || 'sentry.io',
protocol: process.env.SENTRY_PROTOCOL || 'https',
project_id: process.env.SENTRY_PROJECT_ID || '',
Expand All @@ -28,28 +26,20 @@ module.exports = function sentry (moduleOptions) {
}

// Don't proceed if no keys are provided
if (!options.dsn && !options.public_key && !options.private_key) {
logger.info('Disabled because no (private) key was found')
if (!options.dsn && !options.public_key) {
logger.info('Disabled because no key was found')
return
}

// Generate DSN
// https://docs.sentry.io/quickstart/#about-the-dsn
if (!options.dsn || !options.dsn.length) {
if (!options.private_key || !options.private_key.length) {
options.dsn = `${options.protocol}://${options.public_key}@${options.host}${options.path}${options.project_id}`
} else {
options.dsn = `${options.protocol}://${options.public_key}:${options.private_key}@${options.host}${options.path}${options.project_id}`
}
}

// Public DSN (without private key)
if (!options.public_dsn || !options.public_dsn.length) {
options.public_dsn = options.dsn.replace(/:\w+@/, '@')
options.dsn = `${options.protocol}://${options.public_key}@${options.host}${options.path}${options.project_id}`
}

// Setup raven
Raven.config(options.dsn, options.config).install()
options.config.dsn = options.dsn
// Setup sentry
Sentry.init(options.config)

// Register the client plugin
if (!options.disableClientSide) {
Expand All @@ -62,20 +52,26 @@ module.exports = function sentry (moduleOptions) {
}

if (this.nuxt.hook) {
this.nuxt.hook('render:setupMiddleware', app => app.use(Raven.requestHandler()))
this.nuxt.hook('render:errorMiddleware', app => app.use(Raven.errorHandler()))
this.nuxt.hook('render:setupMiddleware', app => app.use(Sentry.Handlers.requestHandler()))
this.nuxt.hook('render:errorMiddleware', app => app.use(Sentry.Handlers.errorHandler()))
this.nuxt.hook('generate:routeFailed', ({ route, errors }) => {
errors.forEach(({ error }) => Raven.captureException(error, { extra: { route } }))
errors.forEach(({ error }) => Sentry.withScope(scope => {
scope.setExtra('route', route)
Sentry.captureException(error)
}))
})
} else {
this.nuxt.plugin('renderer', renderer => {
renderer.app.use(Raven.requestHandler())
renderer.app.use(Sentry.Handlers.requestHandler())

// Grab Nuxt's original error middleware and overwrite it with our own
const nuxtErrorMiddleware = renderer.errorMiddleware
renderer.errorMiddleware = (err, req, res, next) => {
// Log the error
res.sentry = Raven.captureException(err, { req })
Sentry.withScope(scope => {
scope.setExtra('req', req)
Sentry.captureException(err)
})

// Call Nuxt's original error middleware
nuxtErrorMiddleware.call(renderer, err, req, res, next)
Expand Down
22 changes: 12 additions & 10 deletions lib/templates/sentry-client.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import Vue from 'vue'
import Raven from 'raven-js'
import RavenVue from 'raven-js/plugins/vue'
import * as Sentry from '@sentry/browser'

export default function (ctx, inject) {
// Inject Raven to the context as $raven
ctx.$raven = Raven
inject('raven', Raven)

Raven
.config('<%= options.public_dsn %>', <%= serialize(options.config) %>)
.addPlugin(RavenVue, Vue)
.install()
const opts = Object.assign({}, <%= serialize(options.config) %>, {
integrations: (integrations) => {
integrations.push(new Sentry.Integrations.Vue({ Vue }))
return integrations
}
})
Sentry.init(opts)

// Inject Sentry to the context as $sentry
ctx.$sentry = Sentry
inject('sentry', Sentry)
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
]
},
"dependencies": {
"consola": "^1.4.1",
"raven": "^2.3.0",
"raven-js": "^3.21.0"
"consola": "^1.4.3",
"@sentry/node": "^4.0.6",
"@sentry/browser": "^4.0.6"
},
"devDependencies": {
"nuxt-module-builder": "latest"
Expand Down
1 change: 0 additions & 1 deletion test/fixture/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module.exports = {
modules: [
['@@', {
public_key: 'public_key',
private_key: 'private_key',
project_id: 'project_id',
config: {}
}]
Expand Down
Loading

0 comments on commit da63340

Please sign in to comment.