Commit 02966afd authored by Václav Nosek's avatar Václav Nosek Committed by Tim Neutkens

[with-sentry] example: Make 'utils/sentry.js' more flexible (#6769)

* Make `ctx` optional in `captureException` function

* Make 'release' optional when calling sentry util

* Fix code style
parent 18884676
import App from 'next/app' import App from 'next/app'
import sentry from '../utils/sentry' import sentry from '../utils/sentry'
const { Sentry, captureException } = sentry({ release: process.env.SENTRY_RELEASE }) const { Sentry, captureException } = sentry()
export default class MyApp extends App { export default class MyApp extends App {
constructor () { constructor () {
......
...@@ -40,7 +40,7 @@ const sourcemapsForSentryOnly = token => (req, res, next) => { ...@@ -40,7 +40,7 @@ const sourcemapsForSentryOnly = token => (req, res, next) => {
app.prepare().then(() => { app.prepare().then(() => {
// The app.buildId is only available after app.prepare(), hence why we setup // The app.buildId is only available after app.prepare(), hence why we setup
// here. // here.
const { Sentry } = require('./utils/sentry')({ release: app.buildId }) const { Sentry } = require('./utils/sentry')(app.buildId)
express() express()
// This attaches request information to sentry errors // This attaches request information to sentry errors
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
const Sentry = require('@sentry/node') const Sentry = require('@sentry/node')
const Cookie = require('js-cookie') const Cookie = require('js-cookie')
module.exports = ({ release }) => { module.exports = (release = process.env.SENTRY_RELEASE) => {
const sentryOptions = { const sentryOptions = {
dsn: process.env.SENTRY_DSN, dsn: process.env.SENTRY_DSN,
release, release,
...@@ -31,7 +31,7 @@ module.exports = ({ release }) => { ...@@ -31,7 +31,7 @@ module.exports = ({ release }) => {
return { return {
Sentry, Sentry,
captureException: (err, { req, res, errorInfo, query, pathname }) => { captureException: (err, ctx) => {
Sentry.configureScope((scope) => { Sentry.configureScope((scope) => {
if (err.message) { if (err.message) {
// De-duplication currently doesn't work correctly for SSR / browser errors // De-duplication currently doesn't work correctly for SSR / browser errors
...@@ -43,36 +43,40 @@ module.exports = ({ release }) => { ...@@ -43,36 +43,40 @@ module.exports = ({ release }) => {
scope.setExtra('statusCode', err.statusCode) scope.setExtra('statusCode', err.statusCode)
} }
if (res && res.statusCode) { if (ctx) {
scope.setExtra('statusCode', res.statusCode) const { req, res, errorInfo, query, pathname } = ctx
}
if (process.browser) {
scope.setTag('ssr', false)
scope.setExtra('query', query)
scope.setExtra('pathname', pathname)
// On client-side we use js-cookie package to fetch it if (res && res.statusCode) {
const sessionId = Cookie.get('sid') scope.setExtra('statusCode', res.statusCode)
if (sessionId) {
scope.setUser({ id: sessionId })
} }
} else {
scope.setTag('ssr', true)
scope.setExtra('url', req.url)
scope.setExtra('method', req.method)
scope.setExtra('headers', req.headers)
scope.setExtra('params', req.params)
scope.setExtra('query', req.query)
// On server-side we take session cookie directly from request if (process.browser) {
if (req.cookies.sid) { scope.setTag('ssr', false)
scope.setUser({ id: req.cookies.sid }) scope.setExtra('query', query)
scope.setExtra('pathname', pathname)
// On client-side we use js-cookie package to fetch it
const sessionId = Cookie.get('sid')
if (sessionId) {
scope.setUser({ id: sessionId })
}
} else {
scope.setTag('ssr', true)
scope.setExtra('url', req.url)
scope.setExtra('method', req.method)
scope.setExtra('headers', req.headers)
scope.setExtra('params', req.params)
scope.setExtra('query', req.query)
// On server-side we take session cookie directly from request
if (req.cookies.sid) {
scope.setUser({ id: req.cookies.sid })
}
} }
}
if (errorInfo) { if (errorInfo) {
Object.keys(errorInfo).forEach(key => scope.setExtra(key, errorInfo[key])) Object.keys(errorInfo).forEach(key => scope.setExtra(key, errorInfo[key]))
}
} }
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment